gpt4 book ai didi

C# 保存图像质量差

转载 作者:太空宇宙 更新时间:2023-11-03 16:25:17 27 4
gpt4 key购买 nike

我在保存图片时遇到问题。使用此代码,我将两张图片放在一起。但是当我保存它们时,质量比原始图片的质量差。

            Image Image1 = Image.FromFile(openFileDialogOpenPicture1.FileName);
Image Image2 = Image.FromFile(openFileDialogOpenPicture2.FileName);

int imageHeight = 0;
if (Image1.Height > Image2.Height)
imageHeight = Image1.Height;
else
imageHeight = Image2.Height;

Bitmap finalImage = new System.Drawing.Bitmap(Image1.Width + Image2.Width, imageHeight);
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage))
{
//set background color
g.Clear(System.Drawing.Color.Black);

//go through images and draw it on the final image
int offset = 0;
g.DrawImage(pictureBoxBackground1.Image, new System.Drawing.Rectangle(offset, 0, Image1.Width, Image1.Height));
offset += Image1.Width;
g.DrawImage(pictureBoxBackground2.Image, new System.Drawing.Rectangle(offset, 0, Image2.Width, Image2.Height));

var eps = new EncoderParameters(1);
eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
var ici = GetEncoderInfo("image/jpeg");
finalImage.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\TestImage.jpg", ici, eps);
}

我也试过将图片保存为未压缩的,但这只会使文件更大,而不会提高质量。提前致谢!

最佳答案

我已经使用了这个 GetEncoder 方法:

private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}

使用 GetEncoder(ImageFormat.Jpeg) 调用它;并删除了对 pictureBoxBackground1&2 的引用(否则它不会构建),据我所知,最终结果是高质量的。

关于C# 保存图像质量差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12782242/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com