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

时间:2011-01-30 11:39:58

标签: c# .net winforms image-manipulation

我想剪切图片的特定部分,并用它来比较裁剪后的图像与HDD中存储的图像。问题是我不知道如何获得源图像的特定部分。我知道要裁剪的图像的位置(X,Y)。

2 个答案:

答案 0 :(得分:18)

这将加载原件并创建一个从(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图像处理功能。

答案 1 :(得分:2)

对于那些需要在img-tag中为其网站使用裁剪图像的人,您需要更多代码(只是建议,因为我自己需要它) 拿上面的代码加上:

 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))" />