C#使用GDI+創(chuàng)建縮略圖實(shí)例
本文實(shí)例講述了C#使用GDI+創(chuàng)建縮略圖的方法,分享給大家供大家參考。具體方法分析如下:
C#的Gdi+還是相當(dāng)好用的。創(chuàng)建縮略圖步驟如下:
1. Image保存圖像,需要一個(gè)CLSID的參數(shù),它可以這樣獲得:
{
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è)置 可以大幅度的減小圖像文件所占磁盤空間
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直接畫縮略尺寸的圖像效果挺好
{
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)文章
Unity Sockect實(shí)現(xiàn)畫面實(shí)時(shí)傳輸案例原理解析
Socket是比較常用的一種通信方式,本文通過案例給大家介紹Unity Sockect實(shí)現(xiàn)畫面實(shí)時(shí)傳輸功能,感興趣的朋友一起看看吧2021-08-08Winform控件Picture實(shí)現(xiàn)圖片拖拽顯示效果
這篇文章主要為大家詳細(xì)介紹了Winform控件Picture實(shí)現(xiàn)圖片拖拽顯示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09漢字轉(zhuǎn)拼音縮寫示例代碼(Silverlight和.NET 將漢字轉(zhuǎn)換成為拼音)
本篇文章主要介紹了漢字轉(zhuǎn)拼音縮寫示例代碼(Silverlight和.NET 將漢字轉(zhuǎn)換成為拼音) 需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-01-01C#后臺(tái)接受前臺(tái)JSON字符串裝換成字典集合處理
本文介紹C#利用Newtonsoft接收前端的JSON字符串,并解析反序列化成字典集合,對(duì)其進(jìn)行處理。2016-04-04