05-31-2011 05:58 AM
Dear comunity,
I am working on vision application, using c# and vision library 2010. I need to fill an object VisionImage with an image from Bitmap class, from memory.
I've already Bitmap object but I've difficult to understand how copy byte[] array bitmap into VisionImage object.
I used this approch:
Bitmap Image2Work;
.
.
.
.
PixelValue2D pv = new PixelValue2D(BmpToBytes(Image2Work)); // BmpToBytes return byte[]
viImg.ArrayToImage(pv);
I can't compile this. PixelValue2D doesn't like byte[] returned from BmpToBytes
any idea ?
thanks
06-01-2011 07:41 AM
Hi,
I found this on the web:
Initialization:
static Int16[,] intArray=new Int16[320,256];
PixelValue2D pxl=new PixelValue2D(intArray);
in the worker method:
_session.Grab(imageViewer.Image,true); //So I can see the image
pxl=imageViewr.Image.ImageToArray();
intArray=pxl.I16;
Bitmap bmp=new Bitmap(320,256,PixelFormat.Format32bppRgb);
for(int y=0;y
{
for(int x=0;x
{
int val=intArray;
int v =
0xff<<24 | (val<<16) | (val<<8) |val; // This is new otherwise the image is blue but is still distorted
bmp.SetPixel(x,y,Color.FromArgb(v);
}
}
pictureBox1.Image=bmp;
I hope that this portion of code could help you.
Have a good evening!
Claudio
06-01-2011 08:58 AM
Where can I find the documentation about PixelValue2D format byte array ?
I need to set ImageVsion oject from Bitmap ojbect.
I tried using follow code, but I've two issues:
PixelValue2D pv = new PixelValue2D(BmpToBytes(Image2Work));
viImg.SetSize(Image2Work.Width, Image2Work.Height);
viImg.ArrayToImage(pv);
private unsafe byte[,] BmpToBytes(Bitmap bmp)
{
BitmapData bData = bmp.LockBits(new Rectangle(new Point(), bmp.Size), ImageLockMode.ReadOnly, bmp.PixelFormat);
unsafe
{ // number of bytes in the bitmap
int byteCount = bData.Stride * bmp.Height;
byte[] buffTmp = new byte[bData.Stride*bmp.Height];
byte[,] bmpBytes = new byte[bData.Stride, bmp.Height];
Marshal.Copy(bData.Scan0, buffTmp, 0, bData.Stride * bmp.Height);
// Copy the locked bytes from memory
int iIdx=0;
for (int ii = bmp.Width - 1; ii > 0; ii--)
{
for (int i = bmp.Height - 1; i > 0; i--)
{
bmpBytes[bmp.Width - ii, i] = buffTmp[i * bmp.Width + ii];
}
}
// don't forget to unlock the bitmap!!
bmp.UnlockBits(bData);
return bmpBytes;
}
}