asp.net中C實現縮放圖片的例子

來源:互聯網
上載者:User

標頭檔:

 代碼如下 複製代碼

#include <atlconv.h>
#include <AtlBase.h>
#include <tchar.h>
#include "gdiplus.h"
#include <GdiPlusGraphics.h>
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")

CPP:

 代碼如下 複製代碼
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 www.111cn.net
 
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
}
 
void DrawMainWindow(Image* img,int w,int h)
{
HDC hDC = GetDC(AfxGetMainWnd()->m_hWnd);
if (hDC)
{
Graphics graphics(hDC);
graphics.DrawImage(img,w,h,img->GetWidth(),img->GetHeight());
ReleaseDC(AfxGetMainWnd()->m_hWnd,hDC);
}
 
}
 
void GetThumbnail(LPCSTR srcFile,LPCSTR dstFile,int width,int height,bool constRatio)
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken,&gdiplusStartupInput,NULL);
 
USES_CONVERSION;
Image srcImage(A2W(srcFile));
Image *dstImage;
 
int srcWidth = srcImage.GetWidth();
int srcHeight = srcImage.GetHeight();
 
if( constRatio ){
if ( width / srcWidth < height / srcHeight ) {
height = width * srcHeight / srcWidth;
}
else {
width = height * srcWidth / srcHeight;
}
}
dstImage = srcImage.GetThumbnailImage(width,height);
 
CLSID imgClsid;
GetEncoderClsid(L"image/jpeg",&imgClsid);
dstImage->Save(A2W(dstFile),&imgClsid,NULL);
 
DrawMainWindow(&srcImage,0,0);
DrawMainWindow(dstImage,0,srcImage.GetHeight()+10);
delete dstImage;
dstImage = NULL;
 
//GdiplusShutdown(gdiplusToken);
}

使用方法:
 

 代碼如下 複製代碼
GetThumbnail("image.jpg","cc.jpg",200,80,TRUE);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.