C#使用GDI+創(chuàng)建縮略圖實例
本文實例講述了C#使用GDI+創(chuàng)建縮略圖的方法,分享給大家供大家參考。具體方法分析如下:
C#的Gdi+還是相當好用的。創(chuàng)建縮略圖步驟如下:
1. Image保存圖像,需要一個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的另外一個參數(shù)EncoderParameters可用于圖像的壓縮*(這是從網(wǎng)上抄下來的)
使用img/jpeg配合encoderParameters.Parameter[0].Value設置 可以大幅度的減小圖像文件所占磁盤空間
EncoderParameters encoderParameters;
//構造編碼參數(shù)列表
//數(shù)組中只包含一個EncoderParameter對象
encoderParameters.Count = 1;
encoderParameters.Parameter[0].Guid = EncoderQuality;
//參數(shù)類型為LONG
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
//只設置一個參數(shù)
encoderParameters.Parameter[0].NumberOfValues = 1;
ULONG quality;
//壓縮JPEG圖片質量為原來的80%
quality = 80;
encoderParameters.Parameter[0].Value = &quality;
3. 關于縮略圖
我使用了一下Image的GetThumbnailImage,發(fā)現(xiàn)對于某些圖像效果很不理想,(顏色較鮮艷的縮略圖效果好點,但是對于那些色差不大整體又暗的圖像效果就差勁了). 這個時候使用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));
......
}
希望本文所述對大家的C#程序設計有所幫助。
相關文章
C#程序調用C++動態(tài)庫(dll文件)遇到的坑及解決
這篇文章主要介紹了C#程序調用C++動態(tài)庫(dll文件)遇到的坑及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
winform中的ListBox和ComboBox綁定數(shù)據(jù)用法實例
這篇文章主要介紹了winform中的ListBox和ComboBox綁定數(shù)據(jù)用法,實例分析了將集合數(shù)據(jù)綁定到ListBox和ComboBox控件的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2014-12-12
C#實現(xiàn)導出數(shù)據(jù)庫數(shù)據(jù)到Excel文件
利用C#編程語言的強大特性和豐富的.NET庫支持,開發(fā)人員可以高效地完成從數(shù)據(jù)庫到Excel文件的數(shù)據(jù)遷移,下面就跟隨小編一起學習一下具體操作吧2024-12-12

