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