.NET Compact Framework 不支持 Image.Clone 方法,可是仍能够复制图像和图像的某些部分。以下的演示例子演示怎样运行以下操作:

  • 定义一个方法以创建位图。

  • 定义一个重载方法以复制位图或位图的一部分。

  • 通过重写窗口的 OnPaint 方法来调用这些方法并向屏幕绘制图像。

创建位图

  • 此方法创建一个位图以进行演示。

 
// Creates a bitmap for copying.
private Bitmap CreateBitmap(int sideSize)
{
Bitmap bmp = new Bitmap(sideSize, sideSize);
Graphics g = Graphics.FromImage(bmp); g.FillEllipse(new SolidBrush(Color.Red), 0, 0, sideSize, sideSize);
g.DrawLine(new Pen(Color.Black), 0, 0, sideSize, sideSize);
g.DrawLine(new Pen(Color.Black), sideSize, 0, 0, sideSize);
g.Dispose(); return bmp;
}

克隆位图

  • 此方法重载採用源位图作为參数并将该位图作为副本返回。

 
// Copies the entire bitmap.
protected Bitmap CopyBitmap(Bitmap source)
{
return new Bitmap(source);
}

复制位图的一部分

  • 此方法重载採用 Rectangle 作为參数以确定要返回的位图部分的尺寸。

 
// Copies a part of a bitmap.
protected Bitmap CopyBitmap(Bitmap source, Rectangle part)
{
Bitmap bmp = new Bitmap(part.Width, part.Height);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(source,0,0,part,GraphicsUnit.Pixel);
g.Dispose();
return bmp;
}

创建、复制和绘制位图

  • 此 OnPaint 方法重载调用方法创建一个位图,然后克隆并复制该位图的一部分。此方法也能够将克隆的位图保存到一个文件里。

 
// Draws the bitmaps on the form.
protected override void OnPaint(PaintEventArgs e)
{
Font arialFont;
Brush blackBrush;
arialFont = new Font("Arial", 10, FontStyle.Regular);
blackBrush = new SolidBrush(Color.Black); // Set the size of the sides of the bitmap,
// and get one-third of it for the center bitmap.
int sidesize = 75;
int third = (int) sidesize/3; // Create bitmap.
source = CreateBitmap(sidesize); // Copy entirely as a clone.
clone = CopyBitmap(source); // Copy the center part of the bitmap.
center = CopyBitmap(source, new Rectangle(third, third, third, third)); // Save the bitmap to a file.
clone.Save("newbitmap.bmp", ImageFormat.Bmp); // Draw the source, clone, and partial
// bitmaps vertically down the screen.
int y = 10; e.Graphics.DrawString("source bitmap:", arialFont, blackBrush, 10, y);
y += 20; e.Graphics.DrawImage(source, 10, y);
y += source.Height + 10; e.Graphics.DrawString("clone bitmap:", arialFont, blackBrush, 10, y);
y += 20; e.Graphics.DrawImage(clone, 10, y);
y += clone.Height + 10; e.Graphics.DrawString("center part of bitmap:", arialFont, blackBrush, 10, y);
y += 20; e.Graphics.DrawImage(center, 10, y);
y += center.Height + 10; // Dispose graphic objects.
arialFont.Dispose();
blackBrush.Dispose();
}

此演示例子须要引用以下的命名空间:

注意,Font 和 Brush 对象在 OnPaint 方法重载中显式释放。由 PaintEventArgs 对象的 Graphics 属性返回的 Graphics 对象将由垃圾回收器销毁,不须要显式释放。

最新文章

  1. 不再为Apache进程淤积、耗尽内存而困扰((转))
  2. “Stamping” PDF Files Downloaded from SharePoint 2010
  3. 【POJ 3176】Cow Bowling
  4. Android Facebook和Twitter分享
  5. Heritrix源码分析(五) 如何让Heritrix在Ecplise等IDE下编程启动(转)
  6. 转】MyEclipse使用总结——修改MyEclipse默认的Servlet和jsp代码模板
  7. C++:构造函数的重载
  8. MySQL性能优化的21个最佳实践
  9. [转] 字符串模式匹配算法——BM、Horspool、Sunday、KMP、KR、AC算法一网打尽
  10. wamp环境网站根目录更改
  11. 在一个RAC集群中最多支持多少节点
  12. 内存管理单元--MMU
  13. TensorFlow-谷歌深度学习库 命令行参数
  14. LOJ 3089 「BJOI2019」奥术神杖——AC自动机DP+0/1分数规划
  15. 图说OOP基础(一)
  16. CUDA加opencv复现导向滤波算法
  17. C++静态库与动态库(比较透彻)
  18. EasyUI表格DataGrid获取数据的方式
  19. xdoj1321----简单搜索
  20. JZYZOJ 2042 多项式逆元 NTT 多项式

热门文章

  1. Delphi的MDI编程中遇到的一个奇怪问题(值得研究的一个问题)
  2. 没有开发者账号,如何解锁wp8设备
  3. (C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令
  4. sed(查找替换) 与awk(提取字段)
  5. Windows Azure 网站上的 WordPress 3.8
  6. C++模板:读入优化
  7. Linux下安装VNC Server
  8. Mysql5.6.24 zip解压缩版配置及修改默认编码方法
  9. POJ1797 Heavy Transportation 【Dijkstra】
  10. Android学习之DragEvent