J
Jeff Lindholm
I have a project that is capturing pictures from my web cam. I can save the
image to a file and
load it back from the file into my image control no problem.
But I would like to either load it into the image control directly, or
into/out of a stream.
Given the GdipSaveImageToStream definition how do I create a parameter for
the stream parameter,
that gets me a usable stream when it is all done?
Or given the following code, any ideas on how to load the bitmap directly
from the twain source?
I have included the working code that saves/reads using a file, the function
definition and the
code that returns a pointer to the pixels of the bitmap.
Any help here would be appreciated.
(Code based on code I found on CodeProject)
[DllImport("gdiplus.dll", ExactSpelling=true, CharSet=CharSet.Unicode)]
internal static extern int GdipSaveImageToStream(IntPtr image, IntPtr
stream, [In] ref Guid Encoder, IntPtr encoderParams);
private void UseFile()
{
ArrayList pics = _source.TransferPictures();
EndingScan();
_source.CloseSrc();
IntPtr image = (IntPtr) pics[ 0 ];
IntPtr pBmp = GlobalLock(image) ;
IntPtr pixelData = GetPixelLocation(pBmp) ;
int st = GdipCreateBitmapFromGdiDib(pBmp, pixelData, ref image) ;
Guid clsid;
// Save to file and load from file
string fileName = String.Format("C:\\temp\\FileName{0}.JPG", _picNo++);
if (!GdiPlus.GetCodecClsid(fileName, out clsid))
{
return;
}
if (File.Exists(fileName)== true)
{
File.Delete(fileName) ;
}
st = GdipSaveImageToFile(image, fileName, ref clsid, IntPtr.Zero);
_picture.Image = Image.FromFile(fileName);
GlobalFree(pBmp) ;
}
protected IntPtr GetPixelLocation( IntPtr bmp )
{
BITMAPINFOHEADER bitmapHeader = new BITMAPINFOHEADER();
Marshal.PtrToStructure( bmp, bitmapHeader );
if( bitmapHeader.biSizeImage == 0 )
{
bitmapHeader.biSizeImage = ((((bitmapHeader.biWidth *
bitmapHeader.biBitCount) + 31) & ~31) >> 3) * bitmapHeader.biHeight;
}
int pixel = bitmapHeader.biClrUsed;
if( (pixel == 0) && (bitmapHeader.biBitCount <= 8) )
{
pixel = 1 << bitmapHeader.biBitCount;
}
pixel= (pixel * 4) + bitmapHeader.biSize + (int) bmp;
return (IntPtr) pixel;
}
image to a file and
load it back from the file into my image control no problem.
But I would like to either load it into the image control directly, or
into/out of a stream.
Given the GdipSaveImageToStream definition how do I create a parameter for
the stream parameter,
that gets me a usable stream when it is all done?
Or given the following code, any ideas on how to load the bitmap directly
from the twain source?
I have included the working code that saves/reads using a file, the function
definition and the
code that returns a pointer to the pixels of the bitmap.
Any help here would be appreciated.
(Code based on code I found on CodeProject)
[DllImport("gdiplus.dll", ExactSpelling=true, CharSet=CharSet.Unicode)]
internal static extern int GdipSaveImageToStream(IntPtr image, IntPtr
stream, [In] ref Guid Encoder, IntPtr encoderParams);
private void UseFile()
{
ArrayList pics = _source.TransferPictures();
EndingScan();
_source.CloseSrc();
IntPtr image = (IntPtr) pics[ 0 ];
IntPtr pBmp = GlobalLock(image) ;
IntPtr pixelData = GetPixelLocation(pBmp) ;
int st = GdipCreateBitmapFromGdiDib(pBmp, pixelData, ref image) ;
Guid clsid;
// Save to file and load from file
string fileName = String.Format("C:\\temp\\FileName{0}.JPG", _picNo++);
if (!GdiPlus.GetCodecClsid(fileName, out clsid))
{
return;
}
if (File.Exists(fileName)== true)
{
File.Delete(fileName) ;
}
st = GdipSaveImageToFile(image, fileName, ref clsid, IntPtr.Zero);
_picture.Image = Image.FromFile(fileName);
GlobalFree(pBmp) ;
}
protected IntPtr GetPixelLocation( IntPtr bmp )
{
BITMAPINFOHEADER bitmapHeader = new BITMAPINFOHEADER();
Marshal.PtrToStructure( bmp, bitmapHeader );
if( bitmapHeader.biSizeImage == 0 )
{
bitmapHeader.biSizeImage = ((((bitmapHeader.biWidth *
bitmapHeader.biBitCount) + 31) & ~31) >> 3) * bitmapHeader.biHeight;
}
int pixel = bitmapHeader.biClrUsed;
if( (pixel == 0) && (bitmapHeader.biBitCount <= 8) )
{
pixel = 1 << bitmapHeader.biBitCount;
}
pixel= (pixel * 4) + bitmapHeader.biSize + (int) bmp;
return (IntPtr) pixel;
}