zw版【转发·台湾nvp系列Delphi例程】HALCON HImage与Bitmap格式转换
zw版【转发·台湾nvp系列Delphi例程】HALCON HImage与Bitmap格式转换
(Delphi Prism)
namespace HImage_Bitmap_Prism;
interface
uses
System.Drawing,
System.Collections,
System.Collections.Generic,
System.Windows.Forms,
System.ComponentModel,
System.Drawing,
System.Drawing.Imaging,
System.Runtime.InteropServices,
HalconDotNet ;
type
/// <summary>
/// Summary description for MainForm.
/// </summary>
MainForm = partial class(System.Windows.Forms.Form)
private
[DllImport('kernel32', EntryPoint := 'RtlMoveMemory', ExactSpelling := true, CharSet := CharSet.Ansi, SetLastError := true)]
class method CopyMemory(Destination: System.Int32; Source: System.Int32; length: System.Int32); external;
method MainForm_Load(sender: System.Object; e: System.EventArgs);
protected
method Dispose(disposing: Boolean); override;
public
constructor;
end;
implementation
{$REGION Construction and Disposition}
constructor MainForm;
begin
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
end;
method MainForm.Dispose(disposing: Boolean);
begin
if disposing then begin
if assigned(components) then
components.Dispose();
//
// TODO: Add custom disposition code here
//
end;
inherited Dispose(disposing);
end;
{$ENDREGION}
method MainForm.MainForm_Load(sender: System.Object; e: System.EventArgs);
begin
var img1: HImage := new HImage();
var pt: IntPtr;
var mwidth: System.Int32;
var mheight: System.Int32;
var mtype: System.String := '';
var img: Bitmap;
var pal: ColorPalette;
var i: System.Int32;
var Alpha: System.Int32 := 255;
var bitmapData: BitmapData;
var rect: Rectangle;
var ptr: array of System.Int32 := new System.Int32[2];
var PixelSize: System.Int32;
img1.ReadImage('clip');
pt := img1.GetImagePointer1(out mtype, out mwidth, out mheight);
img := new Bitmap(mwidth, mheight, PixelFormat.Format8bppIndexed);
pal := img.Palette;
for i :=0 to 255 do
begin
pal.Entries := Color.FromArgb(Alpha, i, i, i);
end;
img.Palette := pal;
rect := new Rectangle(0, 0, mwidth, mheight);
bitmapData := img.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
PixelSize := Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
ptr[0] := bitmapData.Scan0.ToInt32();
ptr[1] := pt.ToInt32();
if mwidth mod 4 = 0 then
CopyMemory(ptr[0], ptr[1], mwidth * mheight * PixelSize)
else
begin
i := 0;
while i < mheight do
begin
ptr[1] := ptr[1] + mwidth;
CopyMemory(ptr[0], ptr[1], mwidth * PixelSize);
ptr[0] := ptr[0] + bitmapData.Stride;
inc(i)
end;
end;
img.UnlockBits(bitmapData);
pictureBox1.Image := img;
img1.Dispose()
end;
end.