簡體   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