c#.net 產生清晰縮圖的解決方案

來源:互聯網
上載者:User

Bitmap類的GetThumbnailImage方法可以從指定的影像檔中產生縮圖,但是這種方式產生的縮圖清晰度不穩定,有時產生的縮圖的品質很差——模糊不清!

為什麼會這樣?為什麼產生的圖片是模糊的?

像JPEG這樣格式的映像可能把縮圖存在同一個檔案中。如果我們使用System.Drawing.Bitmap的GetThumbnailImage方法會檢測檔案中是否存在縮圖,如果找到該縮圖,它就會返回你所設定的寬高的縮圖版本。如果映像的縮圖的版本比你要求的大小要小,就會發生如下問題:產生的縮圖就會變得模糊,我們知道展開一個映像會導致映像品質的下降。

解決方案:

解決方案十分簡單!我們利用System.Drawing.Image對象來裝載源映像,  把映像縮放到你需要的比例,而又能保持高品質,接著儲存就搞定了!

下面通過ASP .NET C#來示範程式碼片段:

 

//你應該包含這些名稱空間

using System.Drawing; 

using System.Drawing.Drawing2D

//下面開始

//本例中假定了兩個變數:

String src="c:/myImages/a.jpg";   //源影像檔的絕對路徑

String dest="c:/myImages/a_th.jpg";    //產生的縮圖影像檔的絕對路徑

int thumbWidth=132;    //要產生的縮圖的寬度

System.Drawing.Image image = System.Drawing.Image.FromFile(src); //利用Image對象裝載源映像

//接著建立一個System.Drawing.Bitmap對象,並設定你希望的縮圖的寬度和高度。

int srcWidth=image.Width;
int srcHeight=image.Height;
int thumbHeight=(srcHeight/srcWidth)*thumbWidth;
Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);

//從Bitmap建立一個System.Drawing.Graphics對象,用來繪製高品質的縮小圖。

System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);

//設定 System.Drawing.Graphics對象的SmoothingMode屬性為HighQuality

gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//下面這個也設成高品質

gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

//下面這個設成High

gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

//把原始映像繪製成上面所設定寬高的縮小圖

System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);

//儲存映像,大功告成!

bmp.Save(dest);

//最後別忘了釋放資源
bmp.Dispose();
image.Dispose();

參考:

[記錄]高品質映像儲存代碼
 

[C#]

/**//// <param name="fileName">映像名</param>
/// <param name="quality">品質</param>
public static void SaveImage(string fileName, int quality)
{
   
    Bitmap  myBitmap= new Bitmap(fileName);

  
    System.Drawing.Imaging.EncoderParameters myEncoderParameters =
        new System.Drawing.Imaging.EncoderParameters(1);

    System.Drawing.Imaging.EncoderParameter myEncoderParameter =
        new System.Drawing.Imaging.EncoderParameter(
        System.Drawing.Imaging.Encoder.Quality, quality);
 
    myEncoderParameters.Param[0] = myEncoderParameter;

 
    System.Drawing.Imaging.ImageCodecInfo myImageCodecInfo;
    myImageCodecInfo = GetEncoderInfo("image/jpeg");

  
    string ext = myImageCodecInfo.FilenameExtension.Split(';')[0];
    ext = System.IO.Path.GetExtension(ext).ToLower();

    string saveName= System.IO.Path.ChangeExtension(fileName, ext);

    //儲存
    myBitmap.Save(saveName, myImageCodecInfo, myEncoderParameters);
}

//擷取MimeType
private static System.Drawing.Imaging.ImageCodecInfo
    GetEncoderInfo(string mineType)
{
   
    System.Drawing.Imaging.ImageCodecInfo [] myEncoders=
        System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
   
    foreach (System.Drawing.Imaging.ImageCodecInfo myEncoder in myEncoders)
        if (myEncoder.MimeType == mineType)
            return myEncoder;
    return null;
}

[VB.NET]

Public Shared  Sub SaveImage()Sub SaveImage(ByVal fileName As String, _
        ByVal quality As Integer)
   
    Dim myBitmap As Bitmap =  New Bitmap(fileName)
 
   
    Dim myEncoderParameters As _
        New System.Drawing.Imaging.EncoderParameters(1)
   
    Dim myEncoderParameter As _
        New System.Drawing.Imaging.EncoderParameter( _
        System.Drawing.Imaging.Encoder.Quality,quality)
   
    myEncoderParameters.Param(0) = myEncoderParameter

   
    Dim myImageCodecInfo As System.Drawing.Imaging.ImageCodecInfo
    myImageCodecInfo = GetEncoderInfo("image/jpeg")
 
   
    Dim ext As String =  myImageCodecInfo.FilenameExtension.Split(";"c)(0)
    ext = System.IO.Path.GetExtension(ext).ToLower()
 
   
    Dim saveName As String = _
        System.IO.Path.ChangeExtension(fileName, ext)
 
    '儲存
    myBitmap.Save(saveName, myImageCodecInfo, myEncoderParameters)
End Sub
 
'擷取MimeType
Private Shared Function GetEncoderInfo()Function GetEncoderInfo(ByVal mineType As String) _
        As System.Drawing.Imaging.ImageCodecInfo
   
    Dim myEncoders() As System.Drawing.Imaging.ImageCodecInfo = _
        System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()
   
    Dim myEncoder As System.Drawing.Imaging.ImageCodecInfo
    For Each myEncoder In myEncoders
        If myEncoder.MimeType = mineType Then
            Return myEncoder
        End If
    Next
    Return Nothing
End Function

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/swazn_yj/archive/2009/08/23/4474589.aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.