PLEASE HELP!! Transparent blitting not working!

N

Nick Jacobson

The following code is used to draw a bitmap on a form with a color key
(i.e. a color that's not drawn). For speed purposes, I'm trying to
call the Win32 API function: TransparentBlt.

But the call is failing (and the image is not drawn), with an error
code of 126: "The specified module could not be found"
(ERROR_MOD_NOT_FOUND).

(The bitmap is loaded from a separate file called "bmp.bmp", any
bitmap renamed to that will do. I'm using Win XP Pro SP2 and
VStudio.NET 2003.)

Thanks a lot for your help!


<<
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class MyForm : Form
{
Bitmap m_bmp;

public static void Main() { Application.Run(new MyForm()); }
public MyForm()
{
m_bmp = (Bitmap)Bitmap.FromFile("bmp.bmp");
}

[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll")]
public static extern int DeleteDC(IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr
hObject);
[DllImport("gdi32.dll")]
public static extern int DeleteObject(IntPtr hObject);
[DllImport("msimg32.dll")]
public static extern int TransparentBlt(IntPtr hdcDest, int
nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc,
int nYSrc, uint clr);

protected override void OnPaint(PaintEventArgs pea)
{
Graphics gfx = pea.Graphics;

IntPtr hBmp = m_bmp.GetHbitmap();
IntPtr hdcPanel = GetDC(this.Handle);
IntPtr hdcBmp = CreateCompatibleDC((IntPtr)null);
IntPtr hdcOld = SelectObject(hdcBmp, hBmp);

TransparentBlt(hdcPanel, 0, 0, 200, 200, hdcBmp, 0, 0, 0);

SelectObject(hdcBmp, hdcOld);
DeleteObject(hBmp);
DeleteDC(hdcBmp);
ReleaseDC(this.Handle, hdcPanel);
}
}
--Nick
 
B

Bern

why would u want to use win32 api?

Nick Jacobson said:
The following code is used to draw a bitmap on a form with a color key
(i.e. a color that's not drawn). For speed purposes, I'm trying to
call the Win32 API function: TransparentBlt.

But the call is failing (and the image is not drawn), with an error
code of 126: "The specified module could not be found"
(ERROR_MOD_NOT_FOUND).

(The bitmap is loaded from a separate file called "bmp.bmp", any
bitmap renamed to that will do. I'm using Win XP Pro SP2 and
VStudio.NET 2003.)

Thanks a lot for your help!


<<
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class MyForm : Form
{
Bitmap m_bmp;

public static void Main() { Application.Run(new MyForm()); }
public MyForm()
{
m_bmp = (Bitmap)Bitmap.FromFile("bmp.bmp");
}

[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll")]
public static extern int DeleteDC(IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr
hObject);
[DllImport("gdi32.dll")]
public static extern int DeleteObject(IntPtr hObject);
[DllImport("msimg32.dll")]
public static extern int TransparentBlt(IntPtr hdcDest, int
nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc,
int nYSrc, uint clr);

protected override void OnPaint(PaintEventArgs pea)
{
Graphics gfx = pea.Graphics;

IntPtr hBmp = m_bmp.GetHbitmap();
IntPtr hdcPanel = GetDC(this.Handle);
IntPtr hdcBmp = CreateCompatibleDC((IntPtr)null);
IntPtr hdcOld = SelectObject(hdcBmp, hBmp);

TransparentBlt(hdcPanel, 0, 0, 200, 200, hdcBmp, 0, 0, 0);

SelectObject(hdcBmp, hdcOld);
DeleteObject(hBmp);
DeleteDC(hdcBmp);
ReleaseDC(this.Handle, hdcPanel);
}
}
--Nick
 
N

Nick Jacobson

Calling the .NET function is too slow. The Win32 API is way faster.


Bern said:
why would u want to use win32 api?

Nick Jacobson said:
The following code is used to draw a bitmap on a form with a color key
(i.e. a color that's not drawn). For speed purposes, I'm trying to
call the Win32 API function: TransparentBlt.

But the call is failing (and the image is not drawn), with an error
code of 126: "The specified module could not be found"
(ERROR_MOD_NOT_FOUND).

(The bitmap is loaded from a separate file called "bmp.bmp", any
bitmap renamed to that will do. I'm using Win XP Pro SP2 and
VStudio.NET 2003.)

Thanks a lot for your help!


<<
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class MyForm : Form
{
Bitmap m_bmp;

public static void Main() { Application.Run(new MyForm()); }
public MyForm()
{
m_bmp = (Bitmap)Bitmap.FromFile("bmp.bmp");
}

[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll")]
public static extern int DeleteDC(IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr
hObject);
[DllImport("gdi32.dll")]
public static extern int DeleteObject(IntPtr hObject);
[DllImport("msimg32.dll")]
public static extern int TransparentBlt(IntPtr hdcDest, int
nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc,
int nYSrc, uint clr);

protected override void OnPaint(PaintEventArgs pea)
{
Graphics gfx = pea.Graphics;

IntPtr hBmp = m_bmp.GetHbitmap();
IntPtr hdcPanel = GetDC(this.Handle);
IntPtr hdcBmp = CreateCompatibleDC((IntPtr)null);
IntPtr hdcOld = SelectObject(hdcBmp, hBmp);

TransparentBlt(hdcPanel, 0, 0, 200, 200, hdcBmp, 0, 0, 0);

SelectObject(hdcBmp, hdcOld);
DeleteObject(hBmp);
DeleteDC(hdcBmp);
ReleaseDC(this.Handle, hdcPanel);
}
}
--Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top