2
我使用Graphics.DrawImage(DrawText())
將DrawText拖拽到Image中。如何減少C#中的圖像大小?
問題是:我只繪製三個文字,但是原始圖像的大小是:226kb,當Save()〜3.45mb時輸出圖像。它太大了。
圖片尺寸:2732 * 3200
。
我只循環我的清單textFileSplit
,而這個清單隻有三個項目。
這是我所有的代碼保存圖像:
foreach (string text in lstTextFromFile)
{
count++;
if (text == "") continue;
Graphics gra = Graphics.FromImage(img);
string st = lstImgAdded.Items[k].Text;
Bitmap bmp = new Bitmap(@"" + st);
bmp = (Bitmap)ResizePanel(bmp, panel2);
panel2.BackgroundImage = bmp;
Graphics gbmp = Graphics.FromImage(bmp);
string[] textFileSplit = text.Split('-');
for (int u = 0; u < textFileSplit.Count(); u++)
{
myColorLabel = activeLabels[u+1].ForeColor;
gbmp.DrawImage(
DrawText(textFileSplit[u], fontType, myColorLabel,
Color.Transparent),
Point.Round(StretchImageSize(new Point(activeLabels[u+1].Location.X, activeLabels[u+1].Location.Y), panel2)));
}
gra.Dispose();
Guid id = Guid.NewGuid();
ScaleImage(bmp, witdhImg, heightImg)
.Save(linkLocation + "\\" + id + "." + imgType,
ImageFormat.Png);
}
在ScaleImage()
類我試圖保持尺寸一樣出身圖片:
public Image ScaleImage(Image image, int maxWidth, int maxHeight)
{
var ratioX = (double)maxWidth/image.Width;
var ratioY = (double)maxHeight/image.Height;
var ratio = Math.Min(ratioX, ratioY);
var ratio2 = Math.Max(ratioX, ratioY);
var newWidth = (int)(image.Width * ratio);
var newHeight = (int)(image.Height * ratio2);
var newImage = new Bitmap(newWidth, newHeight);
Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
return newImage;
}
我用'高模式'檢查過。 「Hight」與「HighQualityBicubic」沒有區別。 – vanloc
最後一件事,試圖通過EncoderParameters低質量保存()方法,答題更新。 –
這是工作。但之前,我用Image.Png保存它,保持透明。我更新了代碼和背景更改爲黑色。 – vanloc