void CMainFrame::SetImage(HANDLE hBitmap,TW_IMAGEINFO& /*info*/) { //<<<TWAIN>>> CDemoDoc *NewDoc=(CDemoDoc*)((CDemoApp*)AfxGetApp())->demoTemplate->OpenDocumentFile(NULL); if (NewDoc) { if (hBitmap){ CxImage *newima = new CxImage(); newima->CreateFromHANDLE(hBitmap); NewDoc->image = newima; } CString s; s.Format(_T("Acquired Image %d"),((CDemoApp*)AfxGetApp())->m_nDocCount++); NewDoc->SetTitle(s); NewDoc->UpdateAllViews(0,WM_USER_NEWIMAGE); NewDoc->UpdateStatusBar(); } }
////////////////////////////////////////////////////////////////////////////// // CMainFrame message handlers void CMainFrame::OnEditPaste() { CDemoDoc *NewDoc=(CDemoDoc*)((CDemoApp*)AfxGetApp())->demoTemplate->OpenDocumentFile(NULL); if (NewDoc) { HANDLE hBitmap=NULL; CBitmap bmpClip; if (OpenClipboard()) hBitmap=GetClipboardData(CF_DIB); if (hBitmap){ CxImage *newima = new CxImage(); newima->CreateFromHANDLE(hBitmap); NewDoc->image = newima; } CloseClipboard(); CString s; s.Format("Clipboard Image %d",((CDemoApp*)AfxGetApp())->m_nDocCount++); NewDoc->SetTitle(s); NewDoc->UpdateAllViews(0,WM_USER_NEWIMAGE); NewDoc->UpdateStatusBar(); } }
////////////////////////////////////////////////////////////////////////////// // CMainFrame message handlers void CMainFrame::OnEditPaste() { CDemoDoc *NewDoc=(CDemoDoc*)((CDemoApp*)AfxGetApp())->demoTemplate->OpenDocumentFile(NULL); if (NewDoc) { if (OpenClipboard()) { HANDLE hData=NULL; if (hData = GetClipboardData(((CDemoApp*)AfxGetApp())->GetCF())){ //custom CxImage object CxImage *newima = new CxImage(); DWORD dwSize = GlobalSize(hData); if (dwSize) { BYTE *lpVoid = (BYTE *)GlobalLock(hData); newima->UnDump(lpVoid); GlobalUnlock(lpVoid); } NewDoc->image = newima; } else if (hData = GetClipboardData(CF_DIB)){ // check if bitmap CxImage *newima = new CxImage(); newima->CreateFromHANDLE(hData); NewDoc->image = newima; } else { #if CXIMAGE_SUPPORT_WMF if (hData = GetClipboardData(CF_ENHMETAFILE)) //check if metafile { HENHMETAFILE hMeta = (HENHMETAFILE)hData; ENHMETAHEADER emh; GetEnhMetaFileHeader(hMeta, sizeof(emh), &emh); int cx,cy; cx = (int)((emh.rclBounds.right - emh.rclBounds.left)/2.54); cy = (int)((emh.rclBounds.bottom - emh.rclBounds.top)/2.54); HDC hDC0 = ::GetDC(0); // screen dc HBITMAP hBitmap = CreateCompatibleBitmap(hDC0, cx, cy); HDC hDC = CreateCompatibleDC(hDC0); // memory dc compatible with screen ::ReleaseDC(0, hDC0); // don't need anymore. get rid of it. if (hDC && hBitmap){ RECT rc = {0,0,cx,cy}; int bpp = ::GetDeviceCaps(hDC, BITSPIXEL); HBITMAP hBitmapOld = (HBITMAP)SelectObject(hDC, hBitmap); // paint the background DWORD dwBack = RGB(255, 255, 255); //GetSysColor(COLOR_WINDOW); DWORD OldColor = SetBkColor(hDC, dwBack); ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL); SetBkColor(hDC, OldColor); // Play the Metafile into Memory DC BOOL bRet = PlayEnhMetaFile(hDC, hMeta, &rc); SelectObject(hDC, hBitmapOld); CxImage *newima = new CxImage(); if(bRet && newima->Create(cx, cy, bpp, CXIMAGE_FORMAT_WMF)){ bRet = GetDIBits(hDC, hBitmap, 0, (UINT)cy, newima->GetBits(), (LPBITMAPINFO)newima->GetDIB(), DIB_RGB_COLORS); NewDoc->image = newima; } else { delete newima; } } if (hBitmap) DeleteObject(hBitmap); if (hDC) DeleteDC(hDC); } #endif } } CloseClipboard(); CString s; s.Format(_T("Clipboard Image %d"),((CDemoApp*)AfxGetApp())->m_nDocCount++); NewDoc->SetTitle(s); NewDoc->UpdateAllViews(0,WM_USER_NEWIMAGE); NewDoc->UpdateStatusBar(); } }
BOOL CTWainHelper::SaveAsPdf(const char* pFileName, std::vector< HANDLE > m_pic_handles) { PDF *p; int image; const char *imagefile = "/pvf/pic.jpg"; /* This is where font/image/PDF input files live. Adjust as necessary. */ const char *searchpath = "./"; /* create a new PDFlib object */ if ((p = PDF_new()) == (PDF *) 0) { printf("Couldn't create PDFlib object (out of memory)!\n"); return FALSE; } PDF_TRY(p){ /* This means we must check return values of load_font() etc. */ PDF_set_parameter(p, "errorpolicy", "return"); /* This line is required to avoid problems on Japanese systems */ PDF_set_parameter(p, "hypertextencoding", "host"); if (PDF_begin_document(p, pFileName, 0, "") == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); return FALSE; } PDF_set_parameter(p, "SearchPath", searchpath); PDF_set_info(p, "Creator", "TWainApp"); PDF_set_info(p, "Author", "Snail"); PDF_set_info(p, "Title", "image sample (C)"); std::vector< HANDLE >::iterator it; for(it = m_pic_handles.begin(); it != m_pic_handles.end(); it++) { //将Handle转换为CxImage对象 HANDLE imageHandle = (HANDLE)(*it); CxImage cxImage; cxImage.CreateFromHANDLE(imageHandle); //cxImage.Save("C:\\bmp.bmp", CXIMAGE_FORMAT_BMP); cxImage.Save(".\\temp\\temp.jpg", CXIMAGE_FORMAT_JPG); char* imagedata = NULL; DWORD size = 0; CFile file(".\\temp\\temp.jpg", CFile::modeRead); size = file.GetLength(); imagedata = new char[size]; memset(imagedata, 0, size); file.Read(imagedata, size); file.Close(); PDF_create_pvf(p, imagefile, 0, (const char*)imagedata, size, ""); image = PDF_load_image(p, "auto", imagefile, 0, ""); if (image == -1) { char logInfo[LOGSIZE]; sprintf(logInfo, "Error: 加载PVF图像数据失败!错误号:%d, %s:%d\r\n", PDF_get_errmsg(p), __FILE__, __LINE__); CTWainHelper::LogMessage(logInfo); if(imagedata) delete imagedata; continue; } /* dummy page size, will be adjusted by PDF_fit_image() */ PDF_begin_page_ext(p, 10, 10, ""); PDF_fit_image(p, image, 0.0, 0.0, "adjustpage dpi {300 300}"); PDF_end_page_ext(p, ""); /* Delete the virtual file to free the allocated memory */ PDF_delete_pvf(p, imagefile, 0); if(imagedata) delete imagedata; } PDF_end_document(p, ""); ::DeleteFile(".\\temp\\temp.jpg"); } PDF_CATCH(p) { char logInfo[LOGSIZE]; sprintf(logInfo, "EXCEPTION: PDFlib接口调用过程发生异常!错误信息:[%d] %s: %s, %s:%d\r\n", PDF_get_errmsg(p), PDF_get_apiname(p), PDF_get_errmsg(p), __FILE__, __LINE__); CTWainHelper::LogMessage(logInfo); PDF_delete(p); return FALSE; } PDF_delete(p); return TRUE; }