生活随笔
收集整理的這篇文章主要介紹了
image 图片生产XPS文件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
由圖片轉(zhuǎn)為XPS文件,需要調(diào)用windows 版本為6.0后的API創(chuàng)建XPS,然后用圖片刷將圖片刷上去即可。
并且如需加入print ticket可以用同樣的方法加載資源,并載入,以下為我昨天參考MSDN上面的例子寫好的源碼,共享之。
[cpp]?view plaincopy
#include?<iostream>?? #include?<windows.h>?? #include?<comdef.h>?? #include?<fstream>?? #include?<stdio.h>?? #include?<windows.h>?? #include?<comdef.h>?? #include?<XpsObjectModel.h>?? ?? #include?<StrSafe.h>?? #include?<shlobj.h>?? ?? using?namespace?std;?? ?? HRESULT??? CreateRectanglePath(?? ????__in??IXpsOMObjectFactory???*xpsFactory,?? ????__in??const?XPS_RECT????????*rect,?? ????__out?IXpsOMPath????????????**rectPath?? )?? {?? ????? ????HRESULT?hr?=?S_OK;?? ?? ????IXpsOMGeometryFigure???????????*rectFigure;?? ????IXpsOMGeometry?????????????????*imageRectGeometry;?? ????IXpsOMGeometryFigureCollection?*geomFigureCollection;?? ?? ?????? ?????? ????XPS_POINT????????????startPoint?=?{rect->x,?rect->y};?? ????XPS_SEGMENT_TYPE?????segmentTypes[3]?=?{?? ????????XPS_SEGMENT_TYPE_LINE,??? ????????XPS_SEGMENT_TYPE_LINE,??? ????????XPS_SEGMENT_TYPE_LINE?? ????};?? ????FLOAT?segmentData[6]?=?{?? ????????rect->x,??????????????rect->y+rect->height,??? ????????rect->x+rect->width,??rect->y+rect->height,??? ????????rect->x+rect->width,??rect->y??? ????};?? ????BOOL?segmentStrokes[3]?=?{?? ????????TRUE,?TRUE,?TRUE?? ????};?? ?? ?????? ?????? ?? ????hr?=?xpsFactory->CreateGeometryFigure(?&startPoint,?&rectFigure?);?? ????hr?=?rectFigure->SetIsClosed(?TRUE?);?? ????hr?=?rectFigure->SetIsFilled(?TRUE?);?? ????hr?=?rectFigure->SetSegments(?3,?6,??? ????????????segmentTypes,?segmentData,?segmentStrokes?);?? ?? ?????? ????hr?=?xpsFactory->CreateGeometry(?&imageRectGeometry?);?? ????hr?=?imageRectGeometry->GetFigures(?&geomFigureCollection?);?? ????hr?=?geomFigureCollection->Append(?rectFigure?);?? ?? ?????? ?????? ????hr?=?xpsFactory->CreatePath(?reinterpret_cast<IXpsOMPath**>(rectPath)?);?? ????hr?=?(*rectPath)->SetGeometryLocal(?imageRectGeometry?);?? ?? ?????? ?????? ?? ????return?hr;?? }?? ?? ?? ?? int?main(?void?){?? ?????? ????float?width?=?793.76f;?? ????float?height?=?1122.56f;?? ?? ?????? ?? ?????? ?? ?????? ????LPCWSTR?imageFileName?=?L"1.jpg";?? ????float?image_width?=?768.0f;?? ????float?image_height?=?1024.0f;?? ?? ?? ????? ? ? ? ?? ?? ????? ? ?? ????XPS_RECT????rect????????=?{0.0f,?0.0f,?width,?height};?? ????XPS_RECT????viewPort????=?{0.0f,?0.0f,?0.0f,?0.0f};?? ????XPS_RECT????viewBox?????=?{0.0f,?0.0f,?image_width,?image_height};??? ?? ????if(?(image_width?>?image_height)?&&?(image_width?>?width?)?){?? ?????????? ????????float?resize_height?=?image_height?*?(?width/image_width);?? ????????float?padding_top?=?(height?-?resize_height)/2?;?? ?? ????????viewPort.y??????=?padding_top;?? ????????viewPort.width??=?width;?? ????????viewPort.height?=?resize_height;?????? ????}?else?if(?(image_height?>?image_width)?&&?(?image_height?>??height)?){?? ????????float?resize_width?=?image_width?*?(height/image_height);?? ????????float?padding_left?=?(width?-?resize_width)?/?2;?? ?? ????????viewPort.x??????=?padding_left;?? ????????viewPort.width??=?resize_width;?? ????????viewPort.height?=?height;?? ????}?else?{?? ?????????? ????????float?padding_left?=?(width?-?image_width)/2;?? ????????float?padding_top??=?(height?-?image_height)/2;?? ?? ????????viewPort.x??????=?padding_left;?? ????????viewPort.y??????=?padding_top;?? ????????viewPort.width??=?image_width;?? ????????viewPort.height?=?image_height;?? ????}?? ?? ?? ????LPCWSTR?XPSFileName?=?L"a.xps";?? ?? ????IXpsOMObjectFactory?*xpsFactory;?? ?? ????HRESULT?hr?=?S_OK;?? ?????? ?????? ????hr?=?CoInitializeEx(0,?COINIT_MULTITHREADED);?? ?? ????hr?=?CoCreateInstance(?? ????????__uuidof(XpsOMObjectFactory),?? ????????NULL,??? ????????CLSCTX_INPROC_SERVER,?? ????????__uuidof(IXpsOMObjectFactory),?? ????????reinterpret_cast<LPVOID*>(&xpsFactory));?? ?? ????if?(SUCCEEDED(hr))?? ????{?? ?? ????????IOpcPartUri???????????????????*opcPartUri?=?NULL;?? ????????IXpsOMPackage?????????????????*xpsPackage?=?NULL;?? ????????IXpsOMDocumentSequence????????*xpsFDS?=?NULL;?? ????????IXpsOMDocumentCollection??????*fixedDocuments?=?NULL;?? ????????IXpsOMDocument????????????????*xpsFD?=?NULL;?? ????????IXpsOMPage????????????????????*xpsPage?=?NULL;?? ????????IXpsOMPageReferenceCollection?*pageRefs?=?NULL;?? ????????IXpsOMPageReference???????????*xpsPageRef?=?NULL;?? ?? ????????XPS_SIZE?pageSize?=?{width,?height};??? ?????? ?????????? ????????hr?=?xpsFactory->CreatePackage(?&xpsPackage?);?? ?? ?????????? ?????????? ????????hr?=?xpsFactory->CreatePartUri(??? ????????????L"/FixedDocumentSequence.fdseq",?&opcPartUri?);?? ????????hr?=?xpsFactory->CreateDocumentSequence(?opcPartUri,?&xpsFDS?);?? ?????????? ????????if?(NULL?!=?opcPartUri)?{opcPartUri->Release();?opcPartUri?=?NULL;}?? ?? ?? ?????????? ????????hr?=?xpsFactory->CreatePartUri(??? ????????????L"/Documents/1/FixedDocument.fdoc",?&opcPartUri?);?? ????????hr?=?xpsFactory->CreateDocument(?opcPartUri,?&xpsFD?);?? ?????????? ????????if?(NULL?!=?opcPartUri)?{opcPartUri->Release();?opcPartUri?=?NULL;}?? ?? ?? ?????????? ????????hr?=?xpsFactory->CreatePartUri(??? ????????????L"/Documents/1/Pages/1.fpage",?&opcPartUri?);?? ????????hr?=?xpsFactory->CreatePage(?? ????????????&pageSize,???????????????????? ????????????L"en-US",????????????????????? ????????????opcPartUri,??????????????????? ????????????&xpsPage);?????????????????? ?????????? ????????if?(NULL?!=?opcPartUri)?{opcPartUri->Release();?opcPartUri?=?NULL;}?? ?? ?????? ????????IStreamPtr?????????????imageStream;??? ????????IOpcPartUri????????????*imagePartUri;?? ????????IXpsOMImageResource????*imageResource;?? ?? ????????hr?=?xpsFactory->CreateReadOnlyStreamOnFile?(??? ????????????????imageFileName,?&imageStream?);?? ?? ????????hr?=?xpsFactory->CreatePartUri(?imageFileName,?&imagePartUri?);?? ?? ????????? ? ? ? ? ? ? ? ? ? ? ? ?? ????????hr?=?xpsFactory->CreateImageResource?(??? ????????????imageStream,?? ????????????XPS_IMAGE_TYPE_JPEG,?? ????????????imagePartUri,?? ????????????&imageResource);?? ?? ????????imagePartUri->Release();?? ????????imageStream->Release();?? ?????????? ?????????? ?????????? ?????????? ?? ?????????? ????????XPS_SIZE????bmpDim?=?{0,0}?;??? ?? ?????????? ????????FLOAT????????dpiX?=?96.0f;?? ????????FLOAT????????dpiY?=?96.0f;?? ?? ?????????? ????????IXpsOMPath?*imageRectPath;?? ????????IXpsOMImageBrush?*imageBrush;?? ????????IXpsOMVisualCollection?*pageVisuals;?? ?? ?????????? ?????????? ?????????? ?????????? ?????????? ?????????? ?? ?????????? ????????hr?=?xpsFactory->CreateImageBrush(imageResource,?&viewBox,?&viewPort,??? ????????????reinterpret_cast<IXpsOMImageBrush**>(&imageBrush));?? ?? ?????????? ?????????? ????????hr?=?CreateRectanglePath(xpsFactory,?&rect,?? ????????????reinterpret_cast<IXpsOMPath**>(&imageRectPath));?? ?? ?????????? ????????LPCWSTR?shortDescText?=?L"short?description?text.";?? ????????hr?=?imageRectPath->SetAccessibilityShortDescription(?shortDescText?);?? ?? ?????????? ????????hr?=?imageRectPath->SetFillBrushLocal(?imageBrush?);?? ?? ?????????? ????????hr?=?xpsPage->GetVisuals(?&pageVisuals?);?? ?? ?????????? ????????hr?=?pageVisuals->Append(?imageRectPath?);?? ?? ?????????? ????????if?(NULL?!=?pageVisuals)?pageVisuals->Release();?? ????????if?(NULL?!=?imageRectPath)?imageRectPath->Release();?? ????????if?(NULL?!=?imageBrush)?imageBrush->Release();?? ?? ?????????? ????????? ? ?? ????????ofstream?ofpt("Job_PT.xml");?? ????????ofpt?<<?"<?xml?version=\"1.0\"?encoding=\"UTF-8\"?>?\?? ????????????<psf:PrintTicket?xmlns:psf=\"http://schemas.microsoft.com/windows/2003/08/printing/printschemaframework\"?xmlns:psk=\"http://schemas.microsoft.com/windows/2003/08/printing/printschemakeywords\"?xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"?xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"?version=\"1\">\?? ??????????????<psf:Feature?name=\"psk:PageOutputColor\">\?? ????????????<psf:Option?name=\"psk:Monochrome\">\?? ????????????<psf:ScoredProperty?name=\"psk:DeviceBitsPerPixel\">\?? ????????????<psf:Value?xsi:type=\"xsd:integer\">24</psf:Value>\?? ????????????</psf:ScoredProperty>\?? ????????????<psf:ScoredProperty?name=\"psk:DriverBitsPerPixel\">\?? ????????????<psf:Value?xsi:type=\"xsd:integer\">24</psf:Value>\?? ????????????</psf:ScoredProperty>\?? ????????????</psf:Option>\?? ????????????</psf:Feature>\?? ????????????</psf:PrintTicket>";?? ????????ofpt.close();?? ????????IStream?????????????????????*ptStream?=?NULL;?? ?? ????????LPCWSTR?printTicketFileName?=?L"Job_PT.xml";?? ????????hr?=?xpsFactory->CreateReadOnlyStreamOnFile?(?? ????????????printTicketFileName,?? ????????????&ptStream);?? ????????LPCWSTR?printTicketPartName?=?L"Metadata/Job_PT.xml";?? ????????hr?=?xpsFactory->CreatePartUri(??? ????????????????printTicketPartName,?&opcPartUri?);?? ?? ????????IXpsOMPrintTicketResource*?printTicketResource;?? ?? ????????xpsFactory->CreatePrintTicketResource(?ptStream,?opcPartUri,?&printTicketResource);?? ????????opcPartUri->Release();?? ?? ????????if?(NULL?!=?ptStream)?ptStream->Release();?? ?? ?? ?????????? ????????hr?=?xpsFactory->CreatePageReference(?&pageSize,?&xpsPageRef?);?? ?? ?????????? ????????hr?=?xpsPackage->SetDocumentSequence(?xpsFDS?);?? ?? ????????xpsFDS->SetPrintTicketResource(?printTicketResource);?? ?? ?????????? ?????????? ????????hr?=?xpsFDS->GetDocuments(?&fixedDocuments?);?? ????????hr?=?fixedDocuments->Append(?xpsFD?);?? ?? ?????????? ?????????? ????????hr?=?xpsFD->GetPageReferences(?&pageRefs?);?? ????????hr?=?pageRefs->Append(?xpsPageRef?);?? ????????hr?=?xpsPageRef->SetPage(?xpsPage?);?? ?? ????????xpsPackage->WriteToFile(?XPSFileName,?NULL,?FILE_ATTRIBUTE_NORMAL,?FALSE);?? ?????????? ????????if?(NULL?!=?xpsPage)?xpsPage->Release();?? ????????if?(NULL?!=?pageRefs)?pageRefs->Release();?? ????????if?(NULL?!=?fixedDocuments)?fixedDocuments->Release();?? ????????if?(NULL?!=?xpsPageRef)?xpsPageRef->Release();?? ????????if?(NULL?!=?xpsFD)?xpsFD->Release();?? ????????if?(NULL?!=?xpsFDS)?xpsFDS->Release();?? ????????if?(NULL?!=?xpsPackage)?xpsPackage->Release();?? ?????????????? ????????????xpsFactory->Release();?? ????}?? ?? ?????? ????CoUninitialize();?? ????return?0;?? ?? } ?
總結(jié)
以上是生活随笔為你收集整理的image 图片生产XPS文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。