Re: Using SetDIBitsToDevice with C#?

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



> .Net Winform uses GDI+ internally, I am not sure if this is the problem.
> Also, SetDIBitstoDevice should be replaced by StretchDIBits API, because it
> is a superset of SetDIBitstoDevice.
>

I'll look into this. I do care A LOT about speed, so if it's slower, I'm not terribly interested (unless it fixes this color map problem).

> Anyway, is it possible for you to create a sample small project to
> demonstrate the problem? Then we may understand the problem better. Thanks
>

See below...

> Best regards,
> Jeffrey Tan
> Microsoft Online Community Support
> ==================================================
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>

Here is some code. Since we're trying to use a couple external DLLs that I cannot post (in public) I cannot give you something that will compile, but here is what my C# guy (who is quite capable) is trying. Again, he gets a window with the bitmap displayed, but the color map is wrong.

-----------------
FILE: BmodeDLL.cs

// all within the same namespace and class BmodeDll

// some constants
public const int NumRGBQUAD = 256;

// some functions
public static void InitializeDIB(ref BmBitmapInfoStruct bmi, int winXSize, int winYSixe)
{ // set up the DIB information just like I would do in C using only
// the win32 API
bmi.bmiHeader.biSize = 0x28;
bmi.bmiHeader.biWidth = winXSize;
bmi.bmiHeader.biHeight = -winYSixe;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 8;
bmi.bmiHeader.biCompression = 0;
bmi.bmiHeader.biXPelsPerMeter = 0;
bmi.bmiHeader.biYPelsPerMeter = 0;
bmi.bmiHeader.biClrUsed = 0;
bmi.bmiHeader.biClrImportant = 0;

// sets up gray scale colormap... we're using a 24-bit display so this works
for(int i=0; i<NumRGBQUAD; i++ )
{
bmi.bmiColors[i].rgbRed = (Byte) i;
bmi.bmiColors[i].rgbBlue = (Byte) i;
bmi.bmiColors[i].rgbGreen = (Byte) i;
bmi.bmiColors[i].rgbReserved = 0;
}
return;
}

------------------
FILE: ShowImage.cs

// initialize the bitmap and initialize it
private unsafe void InitWindow()
{
bmi = new BmodeDll.BmBitmapInfoStruct();
bmi.bmiColors = new BmodeDll.RGBQUAD[BmodeDll.NumRGBQUAD];
BmodeDll.InitializeDIB(ref bmi, (Int32) winXSize, (Int32) winYSize);
// more init stuff...
}

// This is trying to implement what I'd do in C.
// GetDC()
// CreateCompatibleBitmap()
// get a pointer to the bitmap bits for use in the display routine
// ReleaseDC()

// previously,
// hwnd = ImagePanel.Handle;
// in another initialization routine

private unsafe void SetupBitmap4CLand()
{
try
{
IntPtr hwDC = BmodeDll.GetDC(hwnd); // hwnd is the handle
// to the form
Size clientSize = imagePanel.ClientSize;
IntPtr hData = BmodeDll.CreateCompatibleBitmap(hwDC,
bm.bmWidth, bm.bmHeight);

bm = new BmodeDll.BITMAP();
BmodeDll.GetObject(hData, sizeof (BmodeDll.BITMAP), ref bm);
byBits = new Byte[(bm.bmWidth+16)*(bm.bmHeight+4)];

BmodeDll.ReleaseDC(hwnd, hwDC);
}
// exception processing here...
}

// Now try to draw the bitmap. What's happening in the DLLs is that
// UsbProbeDll.usbCineFramePtr() returns a pointer to an array of bits
// that will be used to calculate the display image.
// Then BmodeDll.bmDrawImage will use those bits, the DIB and such to form
// the image and display it in the window contained in hwnd.
//
// bmDrawImage returns bm (the device dependent bitmap) as well,
// so we might be able to use that,
// but this is a real-time imaging application and we would rather have finer
// control than passing this data around and letting .NET display it - previous
// experience with this method has proved to be (much) slower,
// but that was in earlier
// versions of .NET, VS, C++ and such... We use this for things like saving
// a bitmap to disk.

public unsafe void DrawBmodeImageCLand()
{
try
{
bsptr = UsbProbeDll.usbCineFramePtr();
fixed (byte* pByBits = byBits)
{
BmodeDll.bmDrawImage(hwnd, bsptr, bm, pByBits, ref bmi);
}
}

// exception processing here...
}

I hope this helps. I'm new to the C# world so let me know what else you might want to know and I'll try to get you that.

Regards,
Dave
.



Relevant Pages

  • Re: Displaying a stored image on a form
    ... Embedded bitmap images appear as "Package" instead of "Bitmap ... to display Bitmap images. ... Whatever the original program was that served as the OLE Server to ... I then created a form on which I want to display my graphic file. ...
    (microsoft.public.access.forms)
  • Re: seperator error in __mask_snprintf_len
    ... This patch captures what I am looking for in bitmap display and input. ... * bitmap_snprintf - convert bitmap to an ASCII hex string. ... * and a length, describing the mask contents. ...
    (Linux-Kernel)
  • Re: PictureBox max width
    ... many seconds of recording and the rate. ... and that's the length of display problem. ... what I don't understand is why you need to store all this ... no need whatsoever to store the actual bitmap that the data produces. ...
    (microsoft.public.vb.general.discussion)
  • Re: size of Picture Control
    ... in the known universe, or at any time upgrade your machine, change its display, ... But at no time would I consider redrawing the image to "fit ... resource, set the Picture Control to Type ... Bitmap and selected in the Image field the bitmap resource. ...
    (microsoft.public.vc.mfc)
  • PCL printing problem
    ... I am using the code below to blit a 24-color bitmap into a memory ... This device context is eventually stretched onto the ... LPBITMAPINFO bmi; ... StretchDIBits(tmpdc.m_hDC, 0, 0, bm.bmWidth, bm.bmHeight, ...
    (microsoft.public.windowsce.embedded.vc)