程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#根據原圖生成縮略圖

C#根據原圖生成縮略圖

編輯:C#入門知識

/// <summary>
    /// 生成縮略圖
    /// </summary>
    /// <param name="imgBuffer">原圖byte[]</param>
    /// <param name="width">生成的縮略圖寬度</param>
    /// <param name="height">生成的縮略圖高度</param>
    /// <returns></returns>
    private byte[] GenerateThumbImg(byte[] imgBuffer,int width,int height)
    {
        MemoryStream imgStream = null;
        MemoryStream thumbStream = new MemoryStream();;
        System.Drawing.Image img = null;
        System.Drawing.Image thumbImg = null;
        System.Drawing.Graphics g = null;
        try
        {
            imgStream = new MemoryStream(imgBuffer);
            img = System.Drawing.Image.FromStream(imgStream);
            thumbImg = new System.Drawing.Bitmap(img, width, height);
            g = System.Drawing.Graphics.FromImage(thumbImg);
            // 設置畫布的描繪質量
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(thumbImg, 0, 0, width, height);
            /*g.DrawImage(img, new System.Drawing.Rectangle(0, 0, width, height),
                0, 0, width, height, System.Drawing.GraphicsUnit.Pixel);*/
            thumbImg.Save(thumbStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            return thumbStream.ToArray();
        }
        catch(Exception ex)
        {
            return null;
        }
        finally
        {
            if (g != null)
                g.Dispose();
            if (thumbImg != null)
                thumbImg.Dispose();
            if (img != null)
                img.Dispose();
            if (thumbStream != null)
                thumbStream.Close();
            if (imgStream != null)
                imgStream.Close();
        }
    }

    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved