csharp .net vb 复制图像
2024-08-23 18:52:43
.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;
}
克隆位图
此方法重载採用源位图作为參数并将该位图作为副本返回。
复制位图的一部分
此方法重载採用 Rectangle 作为參数以确定要返回的位图部分的尺寸。
创建、复制和绘制位图
此 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();
}
此演示例子须要引用以下的命名空间:
最新文章
- 不再为Apache进程淤积、耗尽内存而困扰((转))
- “Stamping” PDF Files Downloaded from SharePoint 2010
- 【POJ 3176】Cow Bowling
- Android Facebook和Twitter分享
- Heritrix源码分析(五) 如何让Heritrix在Ecplise等IDE下编程启动(转)
- 转】MyEclipse使用总结——修改MyEclipse默认的Servlet和jsp代码模板
- C++:构造函数的重载
- MySQL性能优化的21个最佳实践
- [转] 字符串模式匹配算法——BM、Horspool、Sunday、KMP、KR、AC算法一网打尽
- wamp环境网站根目录更改
- 在一个RAC集群中最多支持多少节点
- 内存管理单元--MMU
- TensorFlow-谷歌深度学习库 命令行参数
- LOJ 3089 「BJOI2019」奥术神杖——AC自动机DP+0/1分数规划
- 图说OOP基础(一)
- CUDA加opencv复现导向滤波算法
- C++静态库与动态库(比较透彻)
- EasyUI表格DataGrid获取数据的方式
- xdoj1321----简单搜索
- JZYZOJ 2042 多项式逆元 NTT 多项式
热门文章
- Delphi的MDI编程中遇到的一个奇怪问题(值得研究的一个问题)
- 没有开发者账号,如何解锁wp8设备
- (C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令
- sed(查找替换) 与awk(提取字段)
- Windows Azure 网站上的 WordPress 3.8
- C++模板:读入优化
- Linux下安装VNC Server
- Mysql5.6.24 zip解压缩版配置及修改默认编码方法
- POJ1797 Heavy Transportation 【Dijkstra】
- Android学习之DragEvent