如何使用c#裁剪图像?
当前回答
比公认的答案更简单的是:
public static Bitmap cropAtRect(this Bitmap b, Rectangle r)
{
using (var nb = new Bitmap(r.Width, r.Height))
{
using (Graphics g = Graphics.FromImage(nb))
{
g.DrawImage(b, -r.X, -r.Y);
return nb;
}
}
}
并且它避免了最简单答案的“内存不足”异常风险。
注意,位图和图形是可分割的,因此使用了using子句。
编辑:我发现这是很好的png保存的位图。Save或Paint.exe,但失败的png保存,如Paint Shop Pro 6 -内容被取代。添加GraphicsUnit。Pixel给出了不同的错误结果。也许只是这些失败的png有问题。
其他回答
这是另一种方法。在我的情况下,我有:
2个数字上下控制(称为LeftMargin和TopMargin) 1图片盒(pictureBox1) 1个按钮,我称之为generate C:\imagenes\myImage.gif
在按钮内部,我有这样的代码:
Image myImage = Image.FromFile(@"C:\imagenes\myImage.gif");
Bitmap croppedBitmap = new Bitmap(myImage);
croppedBitmap = croppedBitmap.Clone(
new Rectangle(
(int)LeftMargin.Value, (int)TopMargin.Value,
myImage.Width - (int)LeftMargin.Value,
myImage.Height - (int)TopMargin.Value),
System.Drawing.Imaging.PixelFormat.DontCare);
pictureBox1.Image = croppedBitmap;
我尝试在Visual studio 2012使用c#。我从这页上找到了答案
这里有一个裁剪图像的简单例子
public Image Crop(string img, int width, int height, int x, int y)
{
try
{
Image image = Image.FromFile(img);
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
bmp.SetResolution(80, 60);
Graphics gfx = Graphics.FromImage(bmp);
gfx.SmoothingMode = SmoothingMode.AntiAlias;
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
gfx.DrawImage(image, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
// Dispose to free up resources
image.Dispose();
bmp.Dispose();
gfx.Dispose();
return bmp;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
如果你在使用AForge。NET:
using(var croppedBitmap = new Crop(new Rectangle(10, 10, 10, 10)).Apply(bitmap))
{
// ...
}
这很简单:
创建一个裁剪大小的新位图对象。 使用图形。为新的位图创建一个图形对象。 使用DrawImage方法将图像绘制到具有负X和负Y坐标的位图上。
使用bmp.SetResolution(形象。HorizontalResolution, image .VerticalResolution);
这可能是必要的,即使你在这里实现了最佳答案 特别是如果你的图像真的很好,分辨率不是96.0
我的测试示例:
static Bitmap LoadImage()
{
return (Bitmap)Bitmap.FromFile( @"e:\Tests\d_bigImage.bmp" ); // here is large image 9222x9222 pixels and 95.96 dpi resolutions
}
static void TestBigImagePartDrawing()
{
using( var absentRectangleImage = LoadImage() )
{
using( var currentTile = new Bitmap( 256, 256 ) )
{
currentTile.SetResolution(absentRectangleImage.HorizontalResolution, absentRectangleImage.VerticalResolution);
using( var currentTileGraphics = Graphics.FromImage( currentTile ) )
{
currentTileGraphics.Clear( Color.Black );
var absentRectangleArea = new Rectangle( 3, 8963, 256, 256 );
currentTileGraphics.DrawImage( absentRectangleImage, 0, 0, absentRectangleArea, GraphicsUnit.Pixel );
}
currentTile.Save(@"e:\Tests\Tile.bmp");
}
}
}
推荐文章
- c#:如何获得一个字符串的第一个字符?
- String类中的什么方法只返回前N个字符?
- 更好的方法将对象转换为int类型
- 我可以将c#字符串值转换为转义字符串文字吗?
- 在c#中转换char到int
- c#中朋友的对等物是什么?
- 我如何量化两幅图像之间的差异?
- 关键字使用virtual+override vs. new
- 在ASP中选择Tag Helper。NET Core MVC
- 如何在没有任何错误或警告的情况下找到构建失败的原因
- 跨线程操作无效:控件“textBox1”从创建它的线程以外的线程访问
- 否ConcurrentList<T>在。net 4.0?
- 在c#中解析字符串为日期时间
- 由Jon Skeet撰写的《Singleton》澄清
- 自定义数字格式字符串始终显示符号