Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取图像(图片)的特定部分_C#_.net_Winforms_Image Manipulation - Fatal编程技术网

C# 获取图像(图片)的特定部分

C# 获取图像(图片)的特定部分,c#,.net,winforms,image-manipulation,C#,.net,Winforms,Image Manipulation,我想剪切图片的特定部分,并使用它将剪切后的图像和存储在HDD中的另一个图像进行比较。问题是我不知道如何获得源图像的特定部分。我知道要裁剪的图像的位置(X,Y)。这将加载原始图像并创建一个裁剪版本,从(0,0)开始,尺寸为64x64 Bitmap original = new Bitmap( @"C:\SomePath" ); Rectangle srcRect = new Rectangle( 0, 0, 64, 64 ); Bitmap cropped = (Bitmap)original.C

我想剪切图片的特定部分,并使用它将剪切后的图像和存储在HDD中的另一个图像进行比较。问题是我不知道如何获得源图像的特定部分。我知道要裁剪的图像的位置(X,Y)。

这将加载原始图像并创建一个裁剪版本,从(0,0)开始,尺寸为64x64

Bitmap original = new Bitmap( @"C:\SomePath" );
Rectangle srcRect = new Rectangle( 0, 0, 64, 64 );
Bitmap cropped = (Bitmap)original.Clone( srcRect, original.PixelFormat );

顺便说一句,您没有指定这是WinForms还是WPF,因此使用WinForms,因为我并不真正了解WPF图像处理功能。

对于那些需要在img标签中为其网站使用裁剪图像的用户,您需要更多的代码(只是提示,因为我自己需要它) 以上面的代码加上这个:

 byte[] imgbytes;    
 using (MemoryStream stream = new MemoryStream())
 {
        cropped.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        imgbytes = stream.ToArray();
 }
 <img src="@String.Format("data:image/png;base64,{0}", Convert.ToBase64String(imgbytes))" />
byte[]imgbytes;
使用(MemoryStream stream=new MemoryStream())
{
保存(stream、System.Drawing.Imaging.ImageFormat.Png);
imgbytes=stream.ToArray();
}

@user574917:您的大写锁定键被卡住。它显示了一个错误:错误1无法将类型“System.Drawing.Image”隐式转换为“System.Drawing.Bitmap”。存在显式转换(是否缺少转换?)抱歉,您需要转换Bitmap.FromFile的返回值,因为该方法来自Image类。我改为只使用构造函数,时间太晚了:)小心-这是可能的,但效率很低。我曾经这样做,使ASP.NET应用程序的速度减慢到爬行速度。我最终创建了一个
BitmapRegion
类,该类将大多数方法委托给原始位图,但共享像素数据。是的,如果您发现这是应用程序中的瓶颈,那么这样做是个好主意。我的示例复制了该图像。