繁体   English   中英

裁剪没有背景的C#图像

[英]Crop Image C# Without Background

我正在尝试使用C#裁剪图像,但是我遇到了一些问题。

我需要裁剪此图像并从顶部删除15个像素: 在此处输入图片说明

我使用了以下代码:

Bitmap myBitmap = new Bitmap(outputFileName);
Rectangle destRectangle = new Rectangle(new Point(0, 15), new Size(myBitmap.Width, myBitmap.Height));
Bitmap bmp = new Bitmap(myBitmap.Width, myBitmap.Height);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(myBitmap, 0, 0, destRectangle, GraphicsUnit.Pixel);
bmp.Save(outputFileNameCut, ImageFormat.Jpeg);

outputFileName是第一个图像的路径,而outputFileNameCut是新图像的路径。 它工作正常,但是当我保存图像时,保存的图像是这样的:

在此处输入图片说明

看来质量也较差。 这两个图像都是.jpg

谢谢

请改用以下代码:

Bitmap myBitmap = new Bitmap(outputFileName);
Rectangle destRectangle = new Rectangle(new Point(0, 15), 
new Size(myBitmap.Width, myBitmap.Height));
Bitmap bmp = new Bitmap(myBitmap.Width, myBitmap.Height - 15);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(myBitmap, 0, 0, destRectangle, GraphicsUnit.Pixel);
bmp.Save(outputFileNameCut, ImageFormat.Jpeg);

附带说明,这可能会使条形码无法使用。 因为您正在修改图像并调整其大小,这可能会在一定程度上干扰某些扫描仪。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM