簡體   English   中英

C# 在運行時調整圖像大小

[英]C# Resize Image at runtime

如果大小超過 1024x768,我想調整圖像大小,然后保存新實例而不是原始實例,但此代碼正在保存原始實例。 我該如何解決?

      private static void ResizeImage(int newWidth, int newHeight, Stream OriginalImage, string targetPath)
    {
        using (var image = System.Drawing.Image.FromStream(OriginalImage))
        {
            var thumbnailImg = new Bitmap(newWidth, newHeight);
            var thumbGraph = Graphics.FromImage(thumbnailImg);
            thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
            thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
            thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
            var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
            thumbGraph.DrawImage(image, imageRectangle);
            thumbnailImg.Save(targetPath, image.RawFormat);
        }
    }

我假設“thumbnailImg”應該是“圖像”的縮小版本。

如果是這樣 - 使用專為此目的設計的Bitmap 構造函數

Bitmap(Image, Int32, Int32)

例如:

var thumbnail = new Bitmap(image, newWidth, newHeight);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM