方法一(get/set pixel)
核心语句:
resultBitmap.SetPixel(x, y, sourceBitmap.GetPixel(offsetX + x, offsetY+y))
/// <summary>
/// get a certain rectangle part of a known graphic
/// </summary>
/// <param name="bitmapPathAndName">path and name of the source graphic</param>
/// <param name="width">width of the part graphic</param>
/// <param name="height">height of the part graphic</param>
/// <param name="offsetX">the width offset in the source graphic</param>
/// <param name="offsetY">the height offset in the source graphic</param>
/// <returns>wanted graphic</returns>
public Bitmap GetPartOfImage(string bitmapPathAndName, int width, int height,int offsetX,int offsetY)
{
Bitmap sourceBitmap = new Bitmap(bitmapPathAndName);
Bitmap resultBitmap = new Bitmap(width, height);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
if (offsetX + x < sourceBitmap.Size.Width & offsetY + y < sourceBitmap.Size.Height)
{
resultBitmap.SetPixel(x, y, sourceBitmap.GetPixel(offsetX + x, offsetY+y));
}
}
}
return resultBitmap;
}
该方法速度较慢
方法二(graphics.drawimage)
核心代码:
Graphics g = Graphics.FromImage(resultBitmap)
g.DrawImage(sourceBitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel)
/// <summary>
/// get a certain rectangle part of a known graphic
/// </summary>
/// <param name="bitmapPathAndName">path and name of the source graphic</param>
/// <param name="width">width of the part graphic</param>
/// <param name="height">height of the part graphic</param>
/// <param name="offsetX">the width offset in the source graphic</param>
/// <param name="offsetY">the height offset in the source graphic</param>
/// <returns>wanted graphic</returns>
public Bitmap GetPartOfImage(string bitmapPathAndName, int width, int height, int offsetX, int offsetY)
{
Bitmap sourceBitmap = new Bitmap(bitmapPathAndName);
Bitmap resultBitmap = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(resultBitmap))
{
Rectangle resultRectangle = new Rectangle(0, 0, Width, height);
Rectangle sourceRectangle = new Rectangle(0+offsetX, 0+offsetY, Width, height);
g.DrawImage(sourceBitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel);
}
return resultBitmap;
}
速度较快,可完全鄙视掉方法一
using (Graphics g = this.CreateGraphics())
{
g.CopyFromScreen(0, 0, 100, 100, new Size(100, 100));
}
分享到:
相关推荐
(C#)OA办公系统源代码(C#)OA办公系统源代码(C#)OA办公系统源代码(C#)OA办公系统源代码(C#)OA办公系统源代码(C#)OA办公系统源代码(C#)OA办公系统源代码(C#)OA办公系统源代码(C#)OA办公系统源代码(C#)OA办公系统源代码...
c#截取屏幕源码c#截取屏幕源码c#截取屏幕源码c#截取屏幕源码
虚拟键盘 c# 源代码
C#源码集合 C#源码集合 C#源代码集合 C#源代码集合 C#源代码集合
二维码生成 C#源代码二维码生成 C#源代码二维码生成 C#源代码二维码生成 C#源代码二维码生成 C#源代码二维码生成 C#源代码二维码生成 C#源代码二维码生成 C#源代码二维码生成 C#源代码二维码生成 C#源代码二维码生成...
由浅入深学C#源代码 由浅入深学C#源代码 由浅入深学C#源代码 由浅入深学C#源代码
c#截取一张图片的部分 或者缩放图片,教你如何加工原始的图片,变成你想要的效果
C#版源代码C#版源代码C#版源代码C#版源代码
C#写的简单的图片浏览器源代码。
C#初学者经典源代码下载合辑 网络收集整理 适合新手直接学习调用
图书馆管理信息系统 C#源代码图书馆管理信息系统 C#源代码
智慧城市 智能家居 智能社区 C#源代码 设计方案
C#编写的五子棋源代码,功能比较全,是初学者不错的参考资料。
C#所有源代码集合,有利于广大的C#Windows窗体开发者,明细的注释,完美的归类!
c#源代码红绿灯程序c#源代码红绿灯程序c#源代码红绿灯程序
打印图片程序,有C#源代码的打印图片程序,有C#源代码的打印图片程序,有C#源代码的
C#文本编辑器源代码 C#源代码 文本编辑器源代码 程序 应用文本编辑 字体 颜色改变 课程练习必备
C#播放器源代码C#播放器源代码C#播放器源代码C#播放器源代码
本源代码为c#俄罗斯方块代码,代码结构简单清晰,没有冗余的文件; 代码注释详细,方便学习,便于修改; 代码下载后可以直接运行; 游戏功能完备,一般俄罗斯方块的功能都有; 本代码适合c#编程新手,更适合修改后...
C#+屏幕录像+源代码C#+屏幕录像+源代码C#+屏幕录像+源代码C#+屏幕录像+源代码C#+屏幕录像+源代码C#+屏幕录像+源代码