#include <windows.h>
#include <gdiplus.h>
#include <commctrl.h>
#include "resource.h"
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")
#pragma comment(lib, "comctl32.lib")
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; // ailure
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 PopFileInitialize(HWND hwnd, OPENFILENAME *pOfn)
{
static TCHAR szFilter[] = TEXT ("JPG File (*.jpg)\0*.jpg\0") ;
pOfn->lStructSize = sizeof (OPENFILENAME) ;
pOfn->hwndOwner = hwnd ;
pOfn->hInstance = NULL ;
pOfn->lpstrFilter = szFilter ;
pOfn->lpstrCustomFilter = NULL ;
pOfn->nMaxCustFilter = 0 ;
pOfn->nFilterIndex = 0 ;
pOfn->lpstrFile = NULL ; // Set in Open and Close functions
pOfn->nMaxFile = MAX_PATH ;
pOfn->lpstrFileTitle = NULL ; // Set in Open and Close functions
pOfn->nMaxFileTitle = MAX_PATH ;
pOfn->lpstrInitialDir = NULL ;
pOfn->lpstrTitle = NULL ;
pOfn->Flags = 0 ; // Set in Open and Close functions
pOfn->nFileOffset = 0 ;
pOfn->nFileExtension = 0 ;
pOfn->lpstrDefExt = TEXT ("jpg") ;
pOfn->lCustData = 0L ;
pOfn->lpfnHook = NULL ;
pOfn->lpTemplateName = NULL ;
}
//弹出“打开”文件对话框窗口
BOOL PopFileOpenDlg(HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
{
OPENFILENAME ofn ;
PopFileInitialize(hwnd, &ofn) ;
ofn.lpstrFile = pstrFileName ;
ofn.lpstrFileTitle = pstrTitleName ;
ofn.Flags = OFN_FILEMUSTEXIST | OFN_EXPLORER ;
return GetOpenFileName(&ofn) ;
}
//弹出“另存为”对话框窗口
BOOL PopFileSaveDlg(HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
{
OPENFILENAME ofn ;
PopFileInitialize(hwnd, &ofn) ;
ofn.lpstrFile = pstrFileName ;
ofn.lpstrFileTitle = pstrTitleName ;
ofn.Flags = OFN_FILEMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST ;
return GetSaveFileName(&ofn) ;
}
//根据Image对象保存为jpg文件
BOOL SaveJpg(Image *pImg, const WCHAR *lpwszPath)
{
CLSID jpgClsid;
if(!pImg || !lpwszPath) return FALSE ;
GetEncoderClsid(L"image/jpeg", &jpgClsid);
if(Ok == pImg->Save(lpwszPath, &jpgClsid, NULL)) return TRUE ;
return FALSE ;
}
//核心函数 lpszDestImg是最终图片保存的路径
BOOL SetJpgThumb(HWND hEditImgSrc, HWND hEditImgThumb, LPCTSTR lpszDestImg)
{
int iSrcWidth, iSrcHeight, i, iThumbWidth, iThumbHeight ; //iThumbWidth是要生成的临时图片的宽度
WCHAR wszPath[MAX_PATH] ;
if(GetWindowTextLength(hEditImgSrc)<=0 || GetWindowTextLength(hEditImgThumb)<=0 || lpszDestImg==NULL) return FALSE ;
GetWindowTextW(hEditImgSrc, wszPath, MAX_PATH) ;
Image imgSrc(wszPath) ;
iSrcWidth = imgSrc.GetWidth() ; //原图宽度
iSrcHeight = imgSrc.GetHeight() ;
GetWindowTextW(hEditImgThumb, wszPath, MAX_PATH) ;
Image imgThumbSrc(wszPath) ;
//计算要生成的缩略图尺寸
if(iSrcWidth>=200 || iSrcHeight>=200)
{
i = max(iSrcWidth, iSrcHeight) ;
i /= 100 ;
iThumbWidth = iSrcWidth / i ;
iThumbHeight = iSrcHeight / i ;
}
else
{
iThumbWidth = iSrcWidth ;
iThumbHeight = iSrcHeight ;
}
Image *pImgThumb = imgThumbSrc.GetThumbnailImage(iThumbWidth, iThumbHeight) ;
SaveJpg(pImgThumb, L"$tempjpg$") ; //保存临时图片文件
delete pImgThumb ;
//读取临时图片文件的数据
HANDLE hFile = CreateFile(TEXT("$tempjpg$"), GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL) ;
if (hFile == INVALID_HANDLE_VALUE) return FALSE ;
DWORD dwSize = GetFileSize(hFile, NULL) ;
LPVOID data = new BYTE[dwSize] ;
ReadFile(hFile, data, dwSize, &dwSize, NULL) ;
CloseHandle(hFile) ;
DeleteFile(TEXT("$tempjpg$")) ; //删除临时图片文件
//设置图片属性项
PropertyItem propItem ;
USHORT us = 0x0002 ;
propItem.id = PropertyTagResolutionUnit; //Unit of measure for the horizontal resolution and the vertical resolution.
propItem.length = sizeof(us) ;
propItem.type = PropertyTagTypeShort;
propItem.value = &us ;
imgSrc.SetPropertyItem(&propItem);
us = 0x0006 ;
propItem.id = PropertyTagThumbnailCompression; //Compression scheme used for thumbnail image data.
propItem.length = sizeof(us) ;
propItem.type = PropertyTagTypeShort;
propItem.value = &us ;
imgSrc.SetPropertyItem(&propItem);
propItem.id = PropertyTagThumbnailData;
propItem.length = dwSize ;
propItem.type = PropertyTagTypeByte;
propItem.value = data ;
imgSrc.SetPropertyItem(&propItem);
delete data ;
#ifdef _UNICODE
return SaveJpg(&imgSrc, lpszDestImg) ;
#else
MultiByteToWideChar(CP_ACP, 0, lpszDestImg, lstrlen(lpszDestImg)+1, wszPath, MAX_PATH);
return SaveJpg(&imgSrc, wszPath) ;
#endif
}
//获取图片的矩形区域,根据两个picture控件获取
BOOL GetCtrlRect(HWND hwndMain, int iCtrlId, LPRECT lprc)
{
POINT pt ;
if(!hwndMain || !lprc) return FALSE ;
GetWindowRect(GetDlgItem(hwndMain, iCtrlId), lprc) ;
pt.x = lprc->left ;
pt.y = lprc->top ;
ScreenToClient(hwndMain, &pt) ;
lprc->right = pt.x + (lprc->right-lprc->left) ;
lprc->bottom = pt.y + (lprc->bottom-lprc->top) ;
lprc->left = pt.x ;
lprc->top = pt.y ;
return TRUE ;
}
//根据Edit控件显示的图片路径,画图
void DrawImage(HWND hwndMain, HWND hEdit, LPRECT lprc)
{
WCHAR wszPath[MAX_PATH] ;
if(!lprc) return ;
GetWindowTextW(hEdit, wszPath, MAX_PATH) ;
Image img(wszPath) ;
Graphics g(hwndMain) ;
g.DrawImage(&img, lprc->left, lprc->top, lprc->right-lprc->left, lprc->bottom-lprc->top) ;
}
BOOL CALLBACK MainDlg(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HINSTANCE hInstance ;
static HWND hEditImgSrc, hEditImgThumb ;
static RECT rcSrc, rcThumbSrc ; //显示区域
HDROP hDrop = NULL ;
TCHAR szPath[MAX_PATH]={0} ; //路径
POINT pt ; //拖拽文件到窗口时,当前的鼠标坐标
HDC hdc ;
HWND hEdit ; //拖拽文件到窗口时,当前鼠标坐标下的窗口句柄
PAINTSTRUCT ps ;
switch(message)
{
case WM_INITDIALOG:
hInstance = (HINSTANCE)lParam ;
DragAcceptFiles(hwnd, TRUE) ; //使窗口支持文件拖拽功能
SetClassLong(hwnd, GCL_HICON, (long)LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_MAIN))) ; //设置图标
hEditImgSrc = GetDlgItem(hwnd, IDC_EDIT_IMGSRC) ;
hEditImgThumb = GetDlgItem(hwnd, IDC_EDIT_IMGTHUMB) ;
GetCtrlRect(hwnd, IDC_IMG_SRC, &rcSrc) ; //获取显示区域
GetCtrlRect(hwnd, IDC_IMG_THUMBSRC, &rcThumbSrc) ;
return TRUE ;
case WM_DROPFILES: //当文件拖放到窗口时
hDrop = (HDROP)wParam ;
DragQueryFile(hDrop, 0, szPath, MAX_PATH) ; //这里我们只支持一个文件
GetCursorPos(&pt) ; //获取当前鼠标坐标
hEdit = WindowFromPoint(pt) ; //根据坐标值取得当前坐标点下的窗口句柄
// 根据句柄比较,看在哪个编辑控件下
if(hEdit == hEditImgSrc)
{
SetWindowText(hEditImgSrc, szPath) ;
InvalidateRect(hwnd, &rcSrc, TRUE) ;
}
else if(hEdit == hEditImgThumb)
{
SetWindowText(hEditImgThumb, szPath) ;
InvalidateRect(hwnd, &rcThumbSrc, TRUE) ;
}
return TRUE ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
if(GetWindowTextLength(hEditImgSrc) > 0)
DrawImage(hwnd, hEditImgSrc, &rcSrc) ;
if(GetWindowTextLength(hEditImgThumb) > 0)
DrawImage(hwnd, hEditImgThumb, &rcThumbSrc) ;
EndPaint (hwnd, &ps) ;
return TRUE ;
case WM_CLOSE:
EndDialog(hwnd, 0) ;
return TRUE ;
case WM_COMMAND:
switch(LOWORD(wParam

Yeah
- 粉丝: 34
最新资源
- 软件需求规格书.pdf
- 新版软件需求说明书.doc
- 3D打印6轴机械臂-单片机开发资源
- 工程项目管理规定汇编(化工工程).doc
- 综合布线6水平子系统工程技术.pptx
- 基于MATLAB的振动模态分析毕业论文.doc
- 网络系统集成课程设计报告校园网组建样本.doc
- 计算机网络的分类教学方案.doc
- 税务计算机信息系统安全管理规定.doc
- 物联网RFID固定资产管理系统解决方案.doc
- 提高我国道路运输网络经济效用的对策研究.docx
- 濮阳市食品安全溯源监管平台二期软件招标技术要求.doc
- 水厂自动化监控系统施工方案.doc
- 企业外贸电子商务新策略.ppt
- 基于嵌入式系统的多摄像头图像采集ppt课件.ppt
- 调达物流及其循环取货路径优化模型与算法实现.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


