RaedFarid

I'm asking how can I insert JPG image in XPS file (i.e. I have a JPG image and I want to create XPS file which consist of one page and has only this image) is this possible

Thanks in advance for helping me in this.

Best regards,

Raed



Re: XML Paper Specification (XPS) JPG to XPS (how can I insert JPG inside XPS)

Herman Widjaja

Raed,

The easiest solution is to print your JPG image to a printer called "Microsoft XPS Document Writer".

The printer is installed by default in Vista.

If you are running XP or Win2003, you need to install Microsoft XPS Essential Pack (http://www.microsoft.com/whdc/xps/viewxps.mspx).

Thanks

herman





Re: XML Paper Specification (XPS) JPG to XPS (how can I insert JPG inside XPS)

RaedFarid

Thanks Herman,

But I need to do this by writing code, I mean how can I do this by wirinting code.

I need to write some code the generates XPS document and I need to insert JPG image in it.

Any one can help int this,

Thanks,

Raed





Re: XML Paper Specification (XPS) JPG to XPS (how can I insert JPG inside XPS)

D Hanson

First, you need to create a System.Windows.Controls.Image with your JPG as the source. Something like this code found on the Image Class webpage of MSDN.

Code Snippet
Image myImage = new Image();
myImage.Width = 200;
BitmapImage myBitmapImage = new BitmapImage();

myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg");

myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();

myImage.Source = myBitmapImage;

Now you you can write this image as the single page of an xps document with an XpsDocumentWriter like so.

Code Snippet

XpsDocument xpsdoc = new XpsDocument("C:\temp\JPG.xps", FileAccess.ReadWrite);

XpsDocumentWriter docWriter = XpsDocument.CreateXpsDocumentWriter(xpsdoc);

docWriter.Write(myImage);

xpsdoc.Close();

Hope this help.

--David





Re: XML Paper Specification (XPS) JPG to XPS (how can I insert JPG inside XPS)

RaedFarid

Thanks David.. I tried that, but the problem that it converts the JPG image to Bitmap data and the size of output file is large.

Is there any way to insert JPG as jpg image without converting it to bitmap data





Re: XML Paper Specification (XPS) JPG to XPS (how can I insert JPG inside XPS)

Herman Widjaja

Raed,

Can you please try this code. This code inserts an image to a page.

Thanks

herman

Code Snippet

static void Main(string[] args)
{
string resFile = "c:\\temp\\sampleimage.jpg";
XpsDocument xpsDoc = new XpsDocument("c:\\temp\\image.xps", FileAccess.ReadWrite);
IXpsFixedDocumentSequenceWriter fds = xpsDoc.AddFixedDocumentSequence();
IXpsFixedDocumentWriter fd = fds.AddFixedDocument();
IXpsFixedPageWriter fp = fd.AddFixedPage();
XpsResource res = null;
XpsResource thumb = null;

res = fp.AddImage(XpsImageType.JpegImageType);
thumb = xpsDoc.AddThumbnail(XpsImageType.JpegImageType);

WriteStream(res.GetStream(), resFile);
WritePageContent(fp.XmlWriter, res);
res.Commit();

WriteStream(thumb.GetStream(), resFile);
thumb.Commit();

fp.Commit();
fd.Commit();
fds.Commit();
xpsDoc.Close();
}

private static void WritePageContent(System.Xml.XmlWriter xmlWriter, XpsResource res)
{
xmlWriter.WriteStartElement("FixedPage");
xmlWriter.WriteAttributeString("xmlns", @"http://schemas.microsoft.com/xps/2005/06");
xmlWriter.WriteAttributeString("Width", "816");
xmlWriter.WriteAttributeString("Height", "1056");
xmlWriter.WriteAttributeString("xml:lang", "en-US");
xmlWriter.WriteStartElement("Canvas");

if (res is XpsImage)
{
xmlWriter.WriteStartElement("Path");
xmlWriter.WriteAttributeString("Data", "M 20,20 L 770,20 770,770 20,770 z");
xmlWriter.WriteStartElement("Path.Fill");
xmlWriter.WriteStartElement("ImageBrush");
xmlWriter.WriteAttributeString("ImageSource", res.Uri.ToString());
xmlWriter.WriteAttributeString("Viewbox", "0,0,750,750");
xmlWriter.WriteAttributeString("ViewboxUnits", "Absolute");
xmlWriter.WriteAttributeString("Viewport", "20,20,750,750");
xmlWriter.WriteAttributeString("ViewportUnits", "Absolute");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
}

xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
}


private static void WriteStream(Stream stream, string resFile)
{
using (FileStream sourceStream = new FileStream(resFile, FileMode.Open, FileAccess.Read))
{
byte[] buf = new byte[1024];
int read = 0;
while ((read = sourceStream.Read(buf, 0, buf.Length)) > 0)
{
stream.Write(buf, 0, read);
}
}
}






Re: XML Paper Specification (XPS) JPG to XPS (how can I insert JPG inside XPS)

RaedFarid

Thank you Herman for helping in this it works, also thanks for all others who help in this.

Raed





Re: XML Paper Specification (XPS) JPG to XPS (how can I insert JPG inside XPS)

Herman Widjaja

Great...





Re: XML Paper Specification (XPS) JPG to XPS (how can I insert JPG inside XPS)

GHFatal

My question refers to David Hanson's code. I tried your code and it works fine if I process the image file once. If I try to process the image again, it gives me an exception stating that the file is being used by another process. Is there a way to free URI access to a file and free the lock The second method by herman I have also tried, but it gives me an error stating that the xps file already have a root when I tried to rewrite to the same xps file.
Thanks,
Giang