Windows Develop Bookmark and Share   
 index > Windows Forms General > Setting a window's content to display an image stored in a saved image file
 

Setting a window's content to display an image stored in a saved image file

I found this useful utility for scanning images using Twain:
http://www.codeproject.com/KB/dotnet/twaindotnet.aspx?msg=2393128#xx2393128xx

Basically a new window is created - contents set via SetDIBitsToDevice call. The utility offers the option of saving the file to various graphics format eg.jpeg bmp etc.

The question is: How easy is it to reset a windows content by reading the saved file jpeg/bmp etc?

I assume I must use SetDIBitsToDevice but have little experience of GDI..
Thanks
ZiggyShort  Thursday, January 17, 2008 2:37 PM

Hello,

Accroding to the MSDN http://msdn2.microsoft.com/en-us/library/ms532346(VS.85).aspx

The SetDIBitsToDevice function sets the pixels in the specified rectangle on the device that is associated with the destination device context using color data from a DIB.

int SetDIBitsToDevice(
HDC
hdc, // handle to DC
int XDest, // x-coord of destination upper-left corner
int YDest, // y-coord of destination upper-left corner DWORD dwWidth, // source rectangle width
DWORD dwHeight, // source rectangle height
int XSrc, // x-coord of source lower-left corner
int YSrc, // y-coord of source lower-left corner
UINT uStartScan, // first scan line in array
UINT cScanLines, // number of scan lines
CONST VOID *lpvBits, // array of DIB bits
CONST BITMAPINFO *lpbmi, // bitmap information
UINT fuColorUse // RGB or palette indexes
);

In order to display the saved file jpeg/bmp with SetDIBitsToDevice API, we need to

1. Initialize the BITMAPINFO
memset(&bmi, 0, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = ulJpgWidth;
bmi.bmiHeader.biHeight = -ulJpgHeight; // top-down image
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 0;
bmi.bmiHeader.biCompression = BI_JPEG;
bmi.bmiHeader.biSizeImage = nJpgImageSize;
2. call the SetDIBitsToDevice.
iRet = SetDIBitsToDevice(hdc,
ulDstX, ulDstY,
ulDstWidth, ulDstHeight,
0, 0,
0, ulJpgHeight,
pvJpgImage,
&bmi,
DIB_RGB_COLORS);

where hdc =e.Graphics.GetHdc(); and pvJpgImage points to a buffer containing the JPEG image, nJpgImageSize is the size of the buffer, ulJpgWidth is the width of the JPEG image and ulJpgHeight is the height of the image.

Hope it helps

Regards,

Jialiang Ge

Jialiang Ge - MSFT  Monday, January 21, 2008 8:18 AM
I was running this on WPF and found the following did the trick also:

Image image = new Image();
Uri uri = new Uri(@"c:\aa\ratsHiRes.bmp");
BitmapImage bitmapImage = new BitmapImage(uri);
image.Source = bitmapImage;
image.MaxWidth = bitmapImage.Width;
image.MaxHeight = bitmapImage.Height;
Content = image;
ZiggyShort  Monday, January 21, 2008 11:07 AM

Hello,

Accroding to the MSDN http://msdn2.microsoft.com/en-us/library/ms532346(VS.85).aspx

The SetDIBitsToDevice function sets the pixels in the specified rectangle on the device that is associated with the destination device context using color data from a DIB.

int SetDIBitsToDevice(
HDC
hdc, // handle to DC
int XDest, // x-coord of destination upper-left corner
int YDest, // y-coord of destination upper-left corner DWORD dwWidth, // source rectangle width
DWORD dwHeight, // source rectangle height
int XSrc, // x-coord of source lower-left corner
int YSrc, // y-coord of source lower-left corner
UINT uStartScan, // first scan line in array
UINT cScanLines, // number of scan lines
CONST VOID *lpvBits, // array of DIB bits
CONST BITMAPINFO *lpbmi, // bitmap information
UINT fuColorUse // RGB or palette indexes
);

In order to display the saved file jpeg/bmp with SetDIBitsToDevice API, we need to

1. Initialize the BITMAPINFO
memset(&bmi, 0, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = ulJpgWidth;
bmi.bmiHeader.biHeight = -ulJpgHeight; // top-down image
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 0;
bmi.bmiHeader.biCompression = BI_JPEG;
bmi.bmiHeader.biSizeImage = nJpgImageSize;
2. call the SetDIBitsToDevice.
iRet = SetDIBitsToDevice(hdc,
ulDstX, ulDstY,
ulDstWidth, ulDstHeight,
0, 0,
0, ulJpgHeight,
pvJpgImage,
&bmi,
DIB_RGB_COLORS);

where hdc =e.Graphics.GetHdc(); and pvJpgImage points to a buffer containing the JPEG image, nJpgImageSize is the size of the buffer, ulJpgWidth is the width of the JPEG image and ulJpgHeight is the height of the image.

Hope it helps

Regards,

Jialiang Ge

Jialiang Ge - MSFT  Monday, January 21, 2008 8:18 AM
I was running this on WPF and found the following did the trick also:

Image image = new Image();
Uri uri = new Uri(@"c:\aa\ratsHiRes.bmp");
BitmapImage bitmapImage = new BitmapImage(uri);
image.Source = bitmapImage;
image.MaxWidth = bitmapImage.Width;
image.MaxHeight = bitmapImage.Height;
Content = image;
ZiggyShort  Monday, January 21, 2008 11:07 AM

You can use google to search for other answers

Custom Search

More Threads

• How to use MeasureCharacterRanges?
• browser control
• Position a form in a Parent Container
• DateTime picker inside the dataGridView
• Windows Service on Windows XP.
• How do I make .NET toolbars float?
• Forms / Web Integration
• detecting when TextBox text scrolling out of the control
• How to detect arrow keys in a TextBox?
• Printing from a Datagrid in VB.Net