C# 保存位图时图像质量不好

C# 保存位图时图像质量不好,c#,bitmapimage,C#,Bitmapimage,我正在使用位图,在将位图作为jpg文件保存到文件系统时遇到一些问题。我需要帮助:我的应用程序如下所示: 1.将JPG图像文件(1)从文件系统加载到bitmap1 2.将位图1绘制到位图2 3.将位图2另存为JPG图像文件(2) 我想以与文件(1)相同的32位深度保存JPG文件(2) 这就是野兽的本性。JPEG是一种有损压缩格式,调整其大小并再次重新压缩永远不会使它变得更好。您是否对图像进行了重新压缩?这不会添加任何信息。@HansPassant:我想保存JPG文件(2),其32位深度与文件(1)

我正在使用位图,在将位图作为jpg文件保存到文件系统时遇到一些问题。我需要帮助:我的应用程序如下所示:

1.将JPG图像文件(1)从文件系统加载到bitmap1

2.将位图1绘制到位图2

3.将位图2另存为JPG图像文件(2)

我想以与文件(1)相同的32位深度保存JPG文件(2)


这就是野兽的本性。JPEG是一种有损压缩格式,调整其大小并再次重新压缩永远不会使它变得更好。您是否对图像进行了重新压缩?这不会添加任何信息。@HansPassant:我想保存JPG文件(2),其32位深度与文件(1)相同。@TaW:我将源图像绘制到目标图像。毫无意义,JPEG编解码器只支持24bpp。如果你在代码中把它当作32bpp,我们看不到,是的,这看起来很不稳定。
    public OrderProcessor(OrderProcessorParams imageProcessorParams)
    {
        if (imageProcessorParams == null)
        {
            throw new ArgumentNullException(nameof(imageProcessorParams));
        }

        this.imageProcessorParams = imageProcessorParams;
    }

    #endregion

    #region Public Methods

    public void Start()
    {
        Bitmap destinationImage = null;
        var exportFolderPath = GetExportFolderPath();
        Directory.CreateDirectory(exportFolderPath);

        using (var sourceImage = Image.FromFile(imageProcessorParams.SourceImagePath))
        {
            destinationImage = GetDestinationImage();

            using (var graphics = Graphics.FromImage(destinationImage))
            {
                SetGraphicSettings(graphics);

                graphics.DrawImage(sourceImage,
                    new Rectangle(0, 0, (int)(imageProcessorParams.Width.ToInch() * imageProcessorParams.Dpi),
                        (int)(imageProcessorParams.Height.ToInch() * imageProcessorParams.Dpi)));

                destinationImage.Save(Path.Combine(exportFolderPath, GetDestinationImageName()), ImageFormat.Jpeg);

            }
        }

        destinationImage.Dispose();
    }

    #endregion

    #region Private Fields

    private readonly OrderProcessorParams imageProcessorParams;

    #endregion

    #region Private Methods

    private Bitmap GetDestinationImage()
    {
        int width = (int)(imageProcessorParams.Width.ToInch() * imageProcessorParams.Dpi);
        int height = (int)((imageProcessorParams.Height.ToInch() + imageProcessorParams.FooterSize.ToInch()) * imageProcessorParams.Dpi);

        var destinationImage = new Bitmap(width, height, PixelFormat.Format64bppPArgb);

        destinationImage.SetResolution(imageProcessorParams.Dpi, imageProcessorParams.Dpi);

        return destinationImage;
    }

    private string GetExportFolderPath()
    {
        var sourceImageFolderPath = new DirectoryInfo(imageProcessorParams.SourceImagePath).Parent.FullName;
        var destinationFolderName = DateTime.Now.ToString("yyyyMMddHHmmss");

        return Path.Combine(sourceImageFolderPath, destinationFolderName);
    }

    private string GetDestinationImageName()
    {
        return
            $"{Path.GetFileName(imageProcessorParams.SourceImagePath)} {imageProcessorParams.Width}X{imageProcessorParams.Height}.{ImageFormat.Jpeg}";
    }


    #endregion

    #region Static Members

    private static void SetGraphicSettings(Graphics graphics)
    {
        graphics.Clear(Color.White);
        graphics.SmoothingMode = SmoothingMode.HighQuality;
        graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
        graphics.CompositingQuality = CompositingQuality.HighQuality;
        graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
    }
    #endregion
}