方法一(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#源代码二维码生成 C#源代码二维码生成 C#源代码二维码生成 C#源代码二维码生成 C#源代码二维码生成 C#源代码二维码生成 C#源代码二维码生成 C#源代码二维码生成 C#源代码二维码生成 C#源代码二维码生成...
在C#编程中,处理图像是一项常见的任务,包括截取图片的部分区域以及对图片进行缩放。本篇文章将深入探讨这两个知识点,旨在帮助你理解如何使用C#来实现这些功能。 首先,我们要介绍的是如何截取图片的部分。这通常...
(C#)OA办公系统源代码(C#)OA办公系统源代码(C#)OA办公系统源代码(C#)OA办公系统源代码(C#)OA办公系统源代码(C#)OA办公系统源代码(C#)OA办公系统源代码(C#)OA办公系统源代码(C#)OA办公系统源代码(C#)OA办公系统源代码...
下面将详细介绍如何使用C#来实现屏幕截取,并提供相关的源代码示例。 首先,我们需要了解在Windows操作系统中,屏幕截取主要涉及到Windows API的使用,特别是`GetDesktopWindow()`函数,它可以获取到桌面窗口的句柄...
这涉及到智能安防、智能照明、智能能源管理等多个领域,通过C#源代码的设计与实现,可以构建出一套完整的解决方案。 在C#源代码的设计方案中,首先,我们需要考虑数据通信层,这是智能家居系统的基础,可能包括Wi-...
中国移动飞信C#源代码 中国移动飞信C#源代码 中国移动飞信C#源代码
本篇将深入探讨“飞鸽传书C#版”的源代码实现,以及如何利用C#语言在Visual Studio 2005环境下复现其部分功能,包括局域网内的多用户聊天和快速文件传输。 一、C#编程环境与Visual Studio 2005 Visual Studio 2005...
c#源代码学习 .
电子时钟C#源代码电子时钟C#源代码电子时钟C#源代码电子时钟C#源代码电子时钟C#源代码电子时钟C#源代码电子时钟C#源代码电子时钟C#源代码电子时钟C#源代码电子时钟C#源代码电子时钟C#源代码
C#源码集合 C#源码集合 C#源代码集合 C#源代码集合 C#源代码集合
c#源代码程序 下载看看吧 一定有帮助的
在本文中,我们将深入探讨基于C#实现的Modbus Slave源代码及其重要性。 首先,让我们理解Modbus协议。Modbus是1979年由Modicon公司(现属于施耐德电气)推出的一种串行通信协议,它允许设备间进行简单、高效的数据...
用于查看用.Net开发的程序的源代码,前提是源代码没有进行加密处理
本资源“eif解压压缩C#源代码”提供了处理特定文件格式(如eif和eip)的C#源代码,这对于开发者来说是宝贵的参考资料。下面将详细介绍这些知识点。 1. C#编程语言: C#由微软公司推出,是一种面向对象的、类型安全...
【标题】"C#绘图源代码"涵盖了C#编程语言在图形绘制方面的应用,主要集中在使用C#的图形库和API进行图形界面的设计与实现。C#是一种面向对象的编程语言,它提供了丰富的功能来支持图形用户界面(GUI)的创建,包括...
C#版源代码C#版源代码C#版源代码C#版源代码
《C#开发的组态软件源代码解析与实践》 在信息技术日新月异的今天,C#作为Microsoft .NET框架的重要编程语言,被广泛应用于各种软件开发领域,其中包括工业控制、自动化系统以及组态软件的开发。"C#开发的组态软件...
《由浅入深学C#源代码》是一本旨在帮助初学者和有一定基础的程序员深入理解C#编程语言的教程。通过阅读这本书,读者可以逐步掌握C#的语法特性、面向对象编程思想以及如何利用C#进行实际项目开发。下面我们将详细探讨...
【超级报表模板设计器C#源代码】是一款专为开发者设计的报表工具,它基于C#编程语言,提供了强大的报表设计和定制能力。这款设计器能够帮助程序员快速构建出复杂的业务报表,提升开发效率,同时减轻了在报表设计上的...
本源代码为c#俄罗斯方块代码,代码结构简单清晰,没有冗余的文件; 代码注释详细,方便学习,便于修改; 代码下载后可以直接运行; 游戏功能完备,一般俄罗斯方块的功能都有; 本代码适合c#编程新手,更适合修改后...