C#读取Win32标准DLL文件中的Bitmap(位图)

来源:互联网 发布:2016 网络新词 编辑:程序博客网 时间:2024/05/13 15:21

C#通过API函数,读取标准的Win32DLL 文件中的 Bitmap位图文件

 

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using System.Drawing;

namespace LoadLibrary
{
    public static class Program
    {
       
        [DllImport("kernel32.dll", EntryPoint = "LoadLibraryA")]
        public static extern IntPtr LoadLibrary(string sLibName);
        [DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
        public static extern int FreeLibrary(IntPtr hLib);

        [DllImport("User32.dll")]
        public static extern IntPtr LoadBitmap(IntPtr hInstance, int uID);

      
        [STAThread]
        static void Main(string[] args)
        {

            IntPtr hDll = LoadLibrary("cards.dll");
            if (hDll == IntPtr.Zero)
            {
                MessageBox.Show("Can't load library!");
                return;
            }
           
            IntPtr hRes = LoadBitmap(hDll, 10);        
            Bitmap bmp = Bitmap.FromHbitmap(hRes);

            FreeLibrary(hDll);
        }
    }

}

//运行环境 VS2008 (.NET)

 

//cards.dll 为Windows自带的扑克牌游戏扑克牌图案