Плохое качество изображения при сохранении растрового изображения - PullRequest
0 голосов
/ 09 ноября 2019

Я работаю с растровым изображением, и у меня возникли проблемы с сохранением растрового изображения в файловую систему в виде файла jpg. Мне нужно помочь: Мое приложение работает следующим образом:

1.Загрузить файл изображения JPG (1) из файловой системы в растровое изображение1.

2.Ввести растровое изображение1 в растровое изображение2.

3.Сохранить растровое изображение 2 в виде файла изображения JPG (2).

Я хочу сохранить файл JPG (2) с глубиной 32 бит, такой же, как файл (1).

    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
}
...