日期:2014-05-17  浏览次数:20974 次

高手来试试,菜鸟不要来-------GDI画图遮罩问题
3张图,一张背景图(相框),一张原图(用户照片),一个遮罩辅助图(黑白图)
目的:黑白图,白色区域显示相框,黑色的部分显示用户照片,3张图都是不规则图形

现存问题:
假设3张图一样
相框和白色区域可以刚好匹配,但是用户的照片不会和黑色区域匹配,用网上找到的位图与或运算后,相框区域一样会出现用户的照片与或运算后的颜色。

代码如下:

C# code
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)