欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#使用GDI+創(chuàng)建縮略圖實(shí)例

 更新時(shí)間:2014年10月11日 15:58:43   投稿:shichen2014  
這篇文章主要介紹了C#使用GDI+創(chuàng)建縮略圖實(shí)例,是C#程序設(shè)計(jì)中非常實(shí)用的技巧,需要的朋友可以參考下

本文實(shí)例講述了C#使用GDI+創(chuàng)建縮略圖的方法,分享給大家供大家參考。具體方法分析如下:

C#的Gdi+還是相當(dāng)好用的。創(chuàng)建縮略圖步驟如下:

1. Image保存圖像,需要一個(gè)CLSID的參數(shù),它可以這樣獲得:

復(fù)制代碼 代碼如下:
int   GetEncoderClsid(const   WCHAR*   format,   CLSID*   pClsid)  
{  
    UINT     num   =   0;                     //   number   of   image   encoders  
    UINT     size   =   0;                   //   size   of   the   image   encoder   array   in   bytes  
    ImageCodecInfo*   pImageCodecInfo   =   NULL;  
    GetImageEncodersSize(&num,   &size);  
    if(size   ==   0)  
        return   -1;     //   Failure  
    pImageCodecInfo   =   (ImageCodecInfo*)(malloc(size));  
    if(pImageCodecInfo   ==   NULL)  
        return   -1;     //   Failure  
    GetImageEncoders(num,   size,   pImageCodecInfo);  
    for(UINT   j   =   0;   j   <   num;   ++j)  
    {  
        if(   wcscmp(pImageCodecInfo[j].MimeType,   format)   ==   0   )  
        {  
            *pClsid   =   pImageCodecInfo[j].Clsid;  
            free(pImageCodecInfo);  
            return   j;     //   Success  
        }          
    }  
    free(pImageCodecInfo);  
    return   -1;     //   Failure  
}

2. Image::Save的另外一個(gè)參數(shù)EncoderParameters可用于圖像的壓縮*(這是從網(wǎng)上抄下來的)

使用img/jpeg配合encoderParameters.Parameter[0].Value設(shè)置 可以大幅度的減小圖像文件所占磁盤空間

復(fù)制代碼 代碼如下:
// 保存到文件 
    EncoderParameters encoderParameters;   
    //構(gòu)造編碼參數(shù)列表   
    //數(shù)組中只包含一個(gè)EncoderParameter對(duì)象   
    encoderParameters.Count = 1;   
    encoderParameters.Parameter[0].Guid = EncoderQuality;   
    //參數(shù)類型為LONG   
    encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;   
    //只設(shè)置一個(gè)參數(shù)   
    encoderParameters.Parameter[0].NumberOfValues = 1;   
    ULONG quality;   
    //壓縮JPEG圖片質(zhì)量為原來的80%   
    quality = 80;   
    encoderParameters.Parameter[0].Value = &quality;
   

3. 關(guān)于縮略圖

我使用了一下Image的GetThumbnailImage,發(fā)現(xiàn)對(duì)于某些圖像效果很不理想,(顏色較鮮艷的縮略圖效果好點(diǎn),但是對(duì)于那些色差不大整體又暗的圖像效果就差勁了). 這個(gè)時(shí)候使用Graphic配合Bitmap直接畫縮略尺寸的圖像效果挺好

復(fù)制代碼 代碼如下:
BOOL QImgProcess::CreateThumb( int cx ,QBuf &out) 

    ASSERT(m_pImg != NULL); 
    // 創(chuàng)建縮略圖 
    int nWidth = m_pImg->GetWidth(); 
    if (cx >= nWidth) 
    { 
        return TRUE; 
    } 
    int nHeight = m_pImg->GetHeight(); 
    int nThumbHeight = nHeight * cx / m_pImg->GetWidth() ; 
    Bitmap bitmap(cx, nThumbHeight, PixelFormat24bppRGB);   
    Graphics graph(&bitmap);  
    graph.DrawImage(m_pImg, Rect(0,0,cx,nThumbHeight)); 
...... 
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論