Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Solution to super slow picture box painting

20 views
Skip to first unread message

Chris Zinn

unread,
Jan 29, 2002, 12:15:30 PM1/29/02
to
We have been hitting some awesome performance hits when putting JPGs, BMP or
whatever in picture boxes and using themin our applications (typically a
splash or logon screen). The images just paint reallllll slow when there
DPI is not exactly 96 (which is almost always). I tried putting some old
school GDI code in place of the paint function to draw the images and the
difference in speed is astounding. Here is what I did:

PS: You can still put controls on top of these controls and it will paint
correctly

Copy this into your Form1 class (or whatever your form class is called)

public class Form1 : System.Windows.Forms.Form
{
public Static int m_iHBITMAP; // Real Windows HANDLE to the bitmap

// Bring in some old school GDI calls
[DllImport("User32.DLL", EntryPoint="LoadImage")]
public static extern int LoadImage(int hInstance, string sResource,
uint iType, int iWidth, int iHeight, uint fOptions);

[DllImport("GDI32.dll", EntryPoint="CreateCompatibleDC")]
public static extern int CreateCompatibleDC(int hDC);

[DllImport("GDI32.dll", EntryPoint="SelectObject")]
public static extern int SelectObject(int hDC, int hObject);

[DllImport("GDI32.dll", EntryPoint="BitBlt")]
public static extern int BitBlt(int hDestDC, int xDest, int yDest,
int wDest, int hDest, int hSourceDC, int xSrc, int ySrc, int rop);

[DllImport("GDI32.dll", EntryPoint="DeleteDC")]
public static extern int DeleteDC(int hDC);
..... rest of the class


Now over ride the paint method of the picturebox (or panel control, the
panel is much better to use by the way; its a lot more light weight).

// This will paint the entire control with the image. If you want just a
piece of the control,
// modify the bitblt line.
private void panel1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
int hDC = (int) e.Graphics.GetHdc();
int mDC = CreateCompatibleDC(hDC);
int oldItem = SelectObject(mDC, m_iHBITMAP);
BitBlt(hDC, 0, 0, panel1.Width, panel1.Height , mDC, 0, 0,
0x00CC0020);
DeleteDC(mDC);
e.Graphics.ReleaseHdc((IntPtr) hDC);
}

0 new messages