怎么在C#中实现图片的打印预览?
展开全部
thumbctl 控件,你试一下。
写到OnPrint中,第一个参数直接用OnPrint中的pDC,第二个参数是图片路径,
第三,四个参数是图片的左上角座标,第五,六个参数是你显示时要求的图片的大小.
#define HIMETRIC_INCH 2540
LPPICTURE gpPicture;
ShowPicture(CDC* pDC,CString m_strBRoute,int x,int y,int width,int height)
{
HANDLE hFile = CreateFile(m_strBRoute, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
_ASSERTE(INVALID_HANDLE_VALUE != hFile);
// 取得文件大小
DWORD dwFileSize = GetFileSize(hFile, NULL);
_ASSERTE(-1 != dwFileSize);
LPVOID pvData = NULL;
//根据文件大小分配内存
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
_ASSERTE(NULL != hGlobal);
pvData = GlobalLock(hGlobal);
_ASSERTE(NULL != pvData);
DWORD dwBytesRead = 0;
//读取文件并存入全局内存
BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
_ASSERTE(FALSE != bRead);
GlobalUnlock(hGlobal);
CloseHandle(hFile);
LPSTREAM pstm = NULL;
// 通过全局内存创建 IStream* 的指针
HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
_ASSERTE(SUCCEEDED(hr) && pstm);
//通过图形文件创建IPicture 对象
if (gpPicture)
gpPicture-> Release();
hr = OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&gpPicture);
_ASSERTE(SUCCEEDED(hr) && gpPicture);
pstm-> Release();
HDC hdc;
hdc=pDC-> GetSafeHdc();
if (gpPicture)
{
// 取得图片的宽和高
long hmWidth;
long hmHeight;
gpPicture-> get_Width(&hmWidth);
gpPicture-> get_Height(&hmHeight);
//宽高转换为象素
int nWidth = MulDiv(hmWidth, GetDeviceCaps(hdc, LOGPIXELSX), HIMETRIC_INCH);
int nHeight = MulDiv(hmHeight, GetDeviceCaps(hdc, LOGPIXELSY), HIMETRIC_INCH);
RECT rc;
GetClientRect(&rc);/*取得客户区*/
gpPicture-> Render(hdc, x,y, (int)height*hmWidth/hmHeight,height, 0, hmHeight, hmWidth, -hmHeight, &rc);
/*显示图片*/
}
}
写到OnPrint中,第一个参数直接用OnPrint中的pDC,第二个参数是图片路径,
第三,四个参数是图片的左上角座标,第五,六个参数是你显示时要求的图片的大小.
#define HIMETRIC_INCH 2540
LPPICTURE gpPicture;
ShowPicture(CDC* pDC,CString m_strBRoute,int x,int y,int width,int height)
{
HANDLE hFile = CreateFile(m_strBRoute, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
_ASSERTE(INVALID_HANDLE_VALUE != hFile);
// 取得文件大小
DWORD dwFileSize = GetFileSize(hFile, NULL);
_ASSERTE(-1 != dwFileSize);
LPVOID pvData = NULL;
//根据文件大小分配内存
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
_ASSERTE(NULL != hGlobal);
pvData = GlobalLock(hGlobal);
_ASSERTE(NULL != pvData);
DWORD dwBytesRead = 0;
//读取文件并存入全局内存
BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
_ASSERTE(FALSE != bRead);
GlobalUnlock(hGlobal);
CloseHandle(hFile);
LPSTREAM pstm = NULL;
// 通过全局内存创建 IStream* 的指针
HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
_ASSERTE(SUCCEEDED(hr) && pstm);
//通过图形文件创建IPicture 对象
if (gpPicture)
gpPicture-> Release();
hr = OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&gpPicture);
_ASSERTE(SUCCEEDED(hr) && gpPicture);
pstm-> Release();
HDC hdc;
hdc=pDC-> GetSafeHdc();
if (gpPicture)
{
// 取得图片的宽和高
long hmWidth;
long hmHeight;
gpPicture-> get_Width(&hmWidth);
gpPicture-> get_Height(&hmHeight);
//宽高转换为象素
int nWidth = MulDiv(hmWidth, GetDeviceCaps(hdc, LOGPIXELSX), HIMETRIC_INCH);
int nHeight = MulDiv(hmHeight, GetDeviceCaps(hdc, LOGPIXELSY), HIMETRIC_INCH);
RECT rc;
GetClientRect(&rc);/*取得客户区*/
gpPicture-> Render(hdc, x,y, (int)height*hmWidth/hmHeight,height, 0, hmHeight, hmWidth, -hmHeight, &rc);
/*显示图片*/
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
水晶报表~~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |