大家好,今天接着上一篇继续讲。
下面介绍相关的方法:
(1)Bitmap.Clone 方法:创建使用指定 PixelFormat定义的此 Bitmap 节的副本。
下面是使用示例:
private void Clone_Example2(PaintEventArgs e)
{
// Create a Bitmap object from a file.
Bitmap myBitmap = new Bitmap("Grapes.jpg");
// Clone a portion of the Bitmap object.
RectangleF cloneRect = new RectangleF(0, 0, 100, 100);
System.Drawing.Imaging.PixelFormat format =
myBitmap.PixelFormat;
Bitmap cloneBitmap = myBitmap.Clone(cloneRect, format);
// Draw the cloned portion of the Bitmap object.
e.Graphics.DrawImage(cloneBitmap, 0, 0);
}
(2)Bitmap.FromHicon(IntPtr) 方法:创建从 Windows 句柄到图标的 Bitmap。
下面是使用示例:
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr LoadImage(int Hinstance,
string name, int type, int width, int height, int load);
private void Hicon_Example(PaintEventArgs e)
{
// Get a handle to an icon.
IntPtr Hicon = LoadImage(0, "smile.ico", 1, 0, 0, 16);
// Create a Bitmap object from the icon handle.
Bitmap iconBitmap = Bitmap.FromHicon(Hicon);
// Draw the Bitmap object to the screen.
e.Graphics.DrawImage(iconBitmap, 0, 0);
}
(3)Bitmap.FromResource(IntPtr, String) 方法:从指定的 Windows 资源创建 Bitmap。
(4)Bitmap.GetHbitmap 方法:从 GDI+ Bitmap 创建 GDI 位图对象。
下面是使用示例:
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
private void DemonstrateGetHbitmap()
{
Bitmap bm = new Bitmap("Picture.jpg");
IntPtr hBitmap = bm.GetHbitmap();
// Do something with hBitmap.
DeleteObject(hBitmap);
}
今天就介绍这几个方法,其它的我们下篇文章再见。