public static bool DrawTransparentBmp(IntPtr hdcDest,
int nXOriginDest,
int nYOriginDest,
int nWidthDest,
int nHeightDest,
IntPtr hdcSrc,
int nXOriginSrc,
int nYOriginSrc,
int nWidthSrc,
int nHeightSrc,
uint crTransparent) { bool isture1=false;
bool isture2 = false;
bool isture3 = false;
bool isture4 = false;
bool isture5 = false;
bool isture6 = false; if (nWidthDest < 1) return false;
if (nWidthSrc < 1) return false;
if (nHeightDest < 1) return false;
if (nHeightSrc < 1) return false; IntPtr hMaskDC,hImageDC;
IntPtr hMaskBitmap,hOldMaskBitmap;
IntPtr hImageBitmap,hOldImageBitmap; //创建mask DC 和临时存源文件的 DC hMaskDC = CreateCompatibleDC(hdcDest); hImageDC = CreateCompatibleDC(hdcDest); //创建mask 位图和image 兼容位图 hMaskBitmap = CreateBitmap(nWidthSrc,nHeightSrc,1,1,null); //单色位图 hImageBitmap = CreateCompatibleBitmap(hImageDC,nWidthSrc,nHeightSrc); //彩色位图 //将位图选进DC中 hOldImageBitmap = SelectObject(hImageDC,hImageBitmap); hOldMaskBitmap = SelectObject(hMaskDC,hMaskBitmap); //将src位图拷入临时的image DC中 if (nWidthDest == nWidthSrc && nHeightDest == nHeightSrc) isture1 = BitBlt(hImageDC, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, SRCCOPY);
else
isture2 = StretchBlt(hImageDC, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, SRCCOPY);
//设置整个需要透明的颜色
SetBkColor(hImageDC,crTransparent); //此步生成掩码位图(mask bitmap)
//整个mask 位图中透明色的地方是白色,其他为黑色
isture3 = BitBlt(hMaskDC, nXOriginDest, nYOriginDest, nWidthSrc, nHeightSrc, hImageDC, nXOriginSrc, nYOriginSrc, SRCCOPY); SetBkColor(hdcDest,RGB(Color.White));
SetTextColor(hdcDest,RGB(Color.Black)); isture4 = BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthSrc, nHeightDest, hImageDC, nXOriginSrc, nYOriginSrc, SRCINVERT); isture5 = BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthSrc, nHeightDest, hMaskDC, nXOriginSrc, nYOriginSrc, SRCAND); isture6 = BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthSrc, nHeightDest, hImageDC, nXOriginSrc, nYOriginSrc, SRCINVERT); //BitBlt(hdcDest,nXOriginDest,nYOriginDest,nWidthSrc,nHeightDest,hMaskDC,0,0,SRCAND); //BitBlt(hdcDest,nXOriginDest,nYOriginDest,nWidthSrc,nHeightDest,hImageDC,0,0,SRCPAINT);
return true;
}
然后在private void button1_Click(object sender, EventArgs e)
{
bool x;
pictureBox3.Image = img;
x=DrawTransparentBmp(pictureBox3.Handle,0,0,pictureBox3.Image.Width,pictureBox3.Image.Height,pictureBox2.Handle,0,0,pictureBox2.Image.Width,pictureBox2.Image.Height,0x12345);
} 调用方法。
在方法DrawTransparentBmp 中。hMaskDC,hImageDC一开始就为0,应该是我实参传递有问题。请教各位了。还有就是bitblt也有问题,只有istrue3是true,其余全是false。
我受伤了。。
int nXOriginDest,
int nYOriginDest,
int nWidthDest,
int nHeightDest,
IntPtr hdcSrc,
int nXOriginSrc,
int nYOriginSrc,
int nWidthSrc,
int nHeightSrc,
uint crTransparent) { bool isture1=false;
bool isture2 = false;
bool isture3 = false;
bool isture4 = false;
bool isture5 = false;
bool isture6 = false; if (nWidthDest < 1) return false;
if (nWidthSrc < 1) return false;
if (nHeightDest < 1) return false;
if (nHeightSrc < 1) return false; IntPtr hMaskDC,hImageDC;
IntPtr hMaskBitmap,hOldMaskBitmap;
IntPtr hImageBitmap,hOldImageBitmap; //创建mask DC 和临时存源文件的 DC hMaskDC = CreateCompatibleDC(hdcDest); hImageDC = CreateCompatibleDC(hdcDest); //创建mask 位图和image 兼容位图 hMaskBitmap = CreateBitmap(nWidthSrc,nHeightSrc,1,1,null); //单色位图 hImageBitmap = CreateCompatibleBitmap(hImageDC,nWidthSrc,nHeightSrc); //彩色位图 //将位图选进DC中 hOldImageBitmap = SelectObject(hImageDC,hImageBitmap); hOldMaskBitmap = SelectObject(hMaskDC,hMaskBitmap); //将src位图拷入临时的image DC中 if (nWidthDest == nWidthSrc && nHeightDest == nHeightSrc) isture1 = BitBlt(hImageDC, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, SRCCOPY);
else
isture2 = StretchBlt(hImageDC, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, SRCCOPY);
//设置整个需要透明的颜色
SetBkColor(hImageDC,crTransparent); //此步生成掩码位图(mask bitmap)
//整个mask 位图中透明色的地方是白色,其他为黑色
isture3 = BitBlt(hMaskDC, nXOriginDest, nYOriginDest, nWidthSrc, nHeightSrc, hImageDC, nXOriginSrc, nYOriginSrc, SRCCOPY); SetBkColor(hdcDest,RGB(Color.White));
SetTextColor(hdcDest,RGB(Color.Black)); isture4 = BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthSrc, nHeightDest, hImageDC, nXOriginSrc, nYOriginSrc, SRCINVERT); isture5 = BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthSrc, nHeightDest, hMaskDC, nXOriginSrc, nYOriginSrc, SRCAND); isture6 = BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthSrc, nHeightDest, hImageDC, nXOriginSrc, nYOriginSrc, SRCINVERT); //BitBlt(hdcDest,nXOriginDest,nYOriginDest,nWidthSrc,nHeightDest,hMaskDC,0,0,SRCAND); //BitBlt(hdcDest,nXOriginDest,nYOriginDest,nWidthSrc,nHeightDest,hImageDC,0,0,SRCPAINT);
return true;
}
然后在private void button1_Click(object sender, EventArgs e)
{
bool x;
pictureBox3.Image = img;
x=DrawTransparentBmp(pictureBox3.Handle,0,0,pictureBox3.Image.Width,pictureBox3.Image.Height,pictureBox2.Handle,0,0,pictureBox2.Image.Width,pictureBox2.Image.Height,0x12345);
} 调用方法。
在方法DrawTransparentBmp 中。hMaskDC,hImageDC一开始就为0,应该是我实参传递有问题。请教各位了。还有就是bitblt也有问题,只有istrue3是true,其余全是false。
我受伤了。。
using (Graphics g = this.CreateGraphics())
{
Bitmap image = ...;
image.MakeTransparent(); //<-------------
g.DrawImageUnscaled(image, 0, 0);
}
http://topic.csdn.net/u/20080505/08/4a678d56-d183-4627-bf26-6f28c3af5a09.html
不能实现啊。
把dllimport部分贴上。请大家帮忙调下。谢谢!
namespace test
{
public partial class Form1 : Form
{
public const int SRCCOPY = 0x00CC0020;//13369376;
public const int SRCAND = 0x008800C6;
public const int SRCPAINT = 0x00EE0086;
public const int SRCINVERT = 0x00660046;
Image img ;
Bitmap bmp; public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
} public static uint RGB(Color color)
{
return (uint)(((uint)color.B << 16) | (ushort)(((ushort)color.G << 8) | color.R));
} private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
[DllImport("user32.dll", EntryPoint = "MessageBoxA")]
public static extern int MsgBox(int hWnd, string msg, string caption, int type);
private void button1_Click(object sender, EventArgs e)
{
bool x;
x=DrawTransparentBmp(pictureBox3.Handle,0,0,pictureBox3.Image.Width,pictureBox3.Image.Height,pictureBox2.Handle,0,0,pictureBox2.Image.Width,pictureBox2.Image.Height,0x12345);
} public static bool DrawTransparentBmp(IntPtr hdcDest,
int nXOriginDest,
int nYOriginDest,
int nWidthDest,
int nHeightDest,
IntPtr hdcSrc,
int nXOriginSrc,
int nYOriginSrc,
int nWidthSrc,
int nHeightSrc,
uint crTransparent) { bool isture1=false;
bool isture2 = false;
bool isture3 = false;
bool isture4 = false;
bool isture5 = false;
bool isture6 = false; if (nWidthDest < 1) return false;
if (nWidthSrc < 1) return false;
if (nHeightDest < 1) return false;
if (nHeightSrc < 1) return false; IntPtr hMaskDC,hImageDC;
IntPtr hMaskBitmap,hOldMaskBitmap;
IntPtr hImageBitmap,hOldImageBitmap; //创建mask DC 和临时存源文件的 DC hMaskDC = CreateCompatibleDC(IntPtr.Zero);//CreateDC("DISPLAY", null, null, null) hImageDC = CreateCompatibleDC(IntPtr.Zero);//CreateDC("DISPLAY", null, null, null) //创建mask 位图和image 兼容位图 hMaskBitmap = CreateBitmap(nWidthSrc,nHeightSrc,1,1,null); //单色位图 hImageBitmap = CreateCompatibleBitmap(hImageDC,nWidthSrc,nHeightSrc); //彩色位图 //将位图选进DC中 hOldImageBitmap = SelectObject(hImageDC,hImageBitmap); hOldMaskBitmap = SelectObject(hMaskDC,hMaskBitmap); //将src位图拷入临时的image DC中 if (nWidthDest == nWidthSrc && nHeightDest == nHeightSrc) isture1 = BitBlt(hImageDC, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, SRCCOPY);
else
isture2 = StretchBlt(hImageDC, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, SRCCOPY);
//设置整个需要透明的颜色
SetBkColor(hImageDC,crTransparent); //此步生成掩码位图(mask bitmap)
//整个mask 位图中透明色的地方是白色,其他为黑色
isture3 = BitBlt(hMaskDC, nXOriginDest, nYOriginDest, nWidthSrc, nHeightSrc, hImageDC, nXOriginSrc, nYOriginSrc, SRCCOPY); SetBkColor(hdcDest,RGB(Color.White));
SetTextColor(hdcDest,RGB(Color.Black)); isture4 = BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthSrc, nHeightDest, hImageDC, nXOriginSrc, nYOriginSrc, SRCINVERT); isture5 = BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthSrc, nHeightDest, hMaskDC, nXOriginSrc, nYOriginSrc, SRCAND); isture6 = BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthSrc, nHeightDest, hImageDC, nXOriginSrc, nYOriginSrc, SRCINVERT);
return true;
} [DllImport("msimg32.dll", EntryPoint = "GetBitmap")]
public static extern string getbitmap(); [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleBitmap")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hdcdest, int nWidthdest, int nHeightdest); [DllImport("gdi32.dll", EntryPoint = "CreateBitmap")]
public static extern IntPtr CreateBitmap(int nWidth,int nHeight, uint cPlanes, uint cBitsPeral, string lpvBits); [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc); [DllImport("gdi32.dll", EntryPoint = "SelectObject")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hbitmap); [DllImport("gdi32.dll", EntryPoint = "BitBlt")]
public static extern bool BitBlt(IntPtr hdcdest, int nXdest, int nYdest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop); [DllImport("gdi32.dll", EntryPoint = "StretchBlt")]
public static extern bool StretchBlt(IntPtr hdcdest, int nXdest, int nYdest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int nsrcWidth, int nsrcHeight, int dwRop);
[DllImport("gdi32.dll", EntryPoint = "SetBkColor")]
public static extern void SetBkColor(IntPtr hdc,uint color); [DllImport("gdi32.dll", EntryPoint = "SetTextColor")]
public static extern void SetTextColor(IntPtr hdc, uint color); [DllImport("gdi32.dll", EntryPoint = "DeleteDC")]
public static extern IntPtr DeleteDC(IntPtr hDc); [DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
public static extern IntPtr DeleteObject(IntPtr bmp); [DllImport("kernel32.dll", EntryPoint = "GetLastError")]
public static extern int GetLastError();
[DllImport("gdi32.dll", EntryPoint = "CreateDC")]
public static extern IntPtr CreateDC(string lpszDrive, string lpszDevice, string lpszOutput, string lplnitData); private void Form1_Load(object sender, EventArgs e)
{
img = Image.FromFile(Application.StartupPath + "\\test.jpg");
bmp = new Bitmap(img);
pictureBox2.Image = img;
}
}
}
你这样跑程序会出问题的,CreateCompatibleDC(), CreateCompatibleBitmap()... 却没有DeleteObject()
画面多画几次你的程序就多泄露几次。你这样写C#, 骨子里还是C++。C#已经用Alpha通道来处理透明了,你再用蒙板不是很麻烦吗。
不过因为alpha不能实现我想要的功能,所以用c++的。
我想要的功能就是去掉背景色,取中间我想要的图像啊。
再有,我发的代码一般都是实现过的。
我用你这个方法试了下。不能完全去掉背景色,没有达到想要的效果。。
那你设alpha值阿。把你的蒙板分值写到alpha通道不就可以了?
{
Bitmap image = new Bitmap(img);
Color backColor = image.GetPixel(1, 1);
image.MakeTransparent(backColor);
g.DrawImageUnscaled(image, 0, 0);
}
这样试了下。背景如果是多颜色,是不可以去掉背景色的啊。麻烦gomoku提示下那个alpha值应该怎么设。