裁剪图像C#无背景

裁剪图像C#无背景,c#,image,C#,Image,我正试图用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.

我正试图用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是第一个映像的路径,outputFileName剪切新映像的路径。它工作正常,但当我保存图像时,保存的图像是这样的:

看来质量也比较差。两幅图片都是.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);

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

不要对此类图像使用JPG。PNG是无损的,非常适合像条形码这样的东西。也就是说,你不是在修剪,而是在重新缩放。您还需要使用源矩形来裁剪图像。当您说“裁剪”时,您的意思是“删除左侧和右侧的空白”吗?另外,看起来您没有处理结果矩形中高度的变化。这不应该是我的位图。高度-15吗?我用过png,但我总是有无损质量