日期:2014-05-17 浏览次数:20974 次
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Reflection; namespace WindowsFormsApplication1 { public partial class Form2 : Form { [DllImport("gdi32.dll")] static public extern IntPtr CreateCompatibleDC(IntPtr hDC); [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("user32.dll", EntryPoint = "GetDC")] public static extern IntPtr GetDC(IntPtr hWnd); [DllImport("gdi32.dll")] public static extern int SetROP2(int h, int op); [DllImport("gdi32.dll")] static public extern bool DeleteObject(IntPtr hObject); [DllImport("gdi32.dll")] static public extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject); [DllImport("gdi32.dll")] static public extern IntPtr DeleteDC(IntPtr hDC); const int SRCAND = 0x8800C6; // (DWORD) dest = source AND dest const int SRCCOPY = 0xCC0020; // (DWORD) dest = source const int SRCERASE = 0x440328; // (DWORD) dest = source AND (NOT dest ) const int SRCINVERT = 0x660046; // (DWORD) dest = source XOR dest const int SRCPAINT = 0xEE0086; // (DWORD) dest = source OR dest const int BLACKNESS = 0x42; const int DSTINVERT = 0x550009; const int MERGECOPY = 0xC000CA; const int MERGEPAINT = 0xBB0226; const int NOTSRCCOPY = 0x330008; const int NOTSRCERASE = 0x1100A6; const int PATCOPY = 0xF00021; const int PATINVERT = 0x5A0049; const int PATPAINT = 0xFB0A09; const int WHITENESS = 0xFF0062; public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { //用户照片 Bitmap BmpForeground = new Bitmap("D:/fore.jpg"); //遮罩图(黑白图) Bitmap BmpFilter = new Bitmap("D:/Bball_mask.png"); //相框 Bitmap BmpBackground = new Bitmap("D:/Bball_frame.png"); Image img = new Bitmap(540, 705); Graphics g = Graphics.FromImage(img); System.IntPtr hdc = g.GetHdc(); System.IntPtr hMemBackground = CreateCompatibleDC(hdc); System.IntPtr hMemFilt = CreateCompatibleDC(hdc); System.IntPtr hMemFore = CreateCompatibleDC(hdc); System.IntPtr hBmpBackground = SelectObject(hMemBackground, BmpBackground.GetHbitmap()); System.IntPtr hBmpFilt = SelectObject(hMemFilt, BmpFilter.GetHbitmap()); System.IntPtr hBmpFore = SelectObject(hMemFore, BmpForeground.GetHbitmap()); BitBlt(hdc, 0, 0, BmpFilter.Width, BmpFilter.Height, hMemFilt, 0, 0, SRCCOPY); BitBlt(hdc, 0, 0, BmpFilter.Width, BmpFilter.Height, hMemBackground, 0, 0, SRCAND); BitBlt(hdc, 0, 0, BmpFilter.Width, BmpFilter.Height, hMemFore, 0, 0, SRCPAINT); SelectObject(hMemBackground, hBmpBackground); SelectObject(hMemFore, hBmpFore); SelectObject(hMemFilt, hBmpFilt); g.ReleaseHdc(); DeleteDC(hMemFore); DeleteDC(hMemFilt); DeleteDC(hMemBackground)