Twain and Stream questions

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jeff Lindholm (jeff_news_at_lindholm.org)
Date: 10/04/04


Date: Mon, 04 Oct 2004 20:10:21 GMT

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;
  }



Relevant Pages

  • Re: Problem with Bitmap::Save
    ... Open the file as a stream, and load the bitmap from the stream. ... this problem, but right now I'm programming with VC++, and I don't want ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Bitmap/Image data type
    ... | It seems that no matter how I load a bitmap into memory it remains ... If I obtained the bitmap from a file the ... | file stays locked or if it came from a stream I can't close the stream. ... | don't see why it is doing this when it loads the full image into memory ...
    (microsoft.public.dotnet.languages.csharp)
  • Set Image Control to Bitmap
    ... want to be able to pull images from the database and load them ... into a bitmap through a stream which I have working. ... I just display the bitmap ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: GdipCreateHBITMAPFromBitmap + PixelFormat32bppARGB
    ... When you call GetHBITMAP and the bitmap has an alpha channel, ... you will get a bluish tint in the areas where the bitmap is transparent. ... public static extern bool AlphaBlend( ... public static extern IntPtr SelectObject ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: VB3 is making my head spin!
    ... > and displaying it pixel by pixel] tell me how. ... compressed bitmap from an original 24 but full colour bitmap that is held on disk as a standard ... It shows you how to load a full colour 24bit .bmp file from disk and count ... Dim z As Long, t1 As Long, t2 As Long ...
    (comp.lang.basic.visual.misc)