PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Format to jpeg in the Pdf
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1825
Page 1 of 1

Author:  lolo818 [ Sun Oct 16, 2011 8:21 pm ]
Post subject:  Format to jpeg in the Pdf

Hello I try to put it in a pdf file of images (one per page) and the problem is that the size of the pdf and the same as the original image.
I try to resize it but nothing did.
I wanted to put these images in PDF format in order to reduce weight but it is not won. :cry:
Thank you for your help
+ + @ Lolo818
Code:
                    PdfDocument document = new PdfDocument();
                    XFont font = new XFont("Times", 15, XFontStyle.Bold);
                    XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.WinAnsi, PdfFontEmbedding.None);
                    PdfPage page = document.AddPage();
                    page.Size = PageSize.A4;
                    XGraphics gfx = XGraphics.FromPdfPage(page);
                    page.Orientation = PageOrientation.Landscape;
                    gfx.DrawString("Photo 1", font,XBrushes.DarkRed, new XRect(0, 0, page.Width, page.Height),XStringFormats.TopCenter);
                    string NomFichier = openFileDialog1.Title.ToString();
                    XImage img1 = Pic1Resize;
                    Rectangle XRect= new Rectangle(0, 0, Pic1Resize.Width, Pic1Resize.Height);
                    gfx.DrawImage(Pic1Resize,XRect, XRect, XGraphicsUnit.Millimeter);

Author:  Thomas Hoevel [ Mon Oct 17, 2011 8:00 am ]
Post subject:  Re: Format to jpeg in the Pdf

Hi!

I'm afraid I don't understand what you try to achieve.
lolo818 wrote:
Code:
Rectangle XRect= new Rectangle(0, 0, Pic1Resize.Width, Pic1Resize.Height);
gfx.DrawImage(Pic1Resize,XRect, XRect, XGraphicsUnit.Millimeter);

AFAIK the method .Width() returns the size of the image in pixels. So I assume your DrawImage call draws the images with one pixel per millimetre. This will not work for large images.

You'd better use
public void DrawImage(XImage image, XRect rect)
or
public void DrawImage(XImage image, int x, int y, int width, int height)
and specify the area where you want the image to appear (all co-ordinates must be given in Points (not pixels). Use class Unit for conversion from e.g. millimetre.

Author:  lolo818 [ Tue Oct 18, 2011 6:02 pm ]
Post subject:  Re: Format to jpeg in the Pdf

Hello, thank you for your help, the PDF file is created is heavier than the original or equal.
Purpose of my code is to reduce the weight of the image in the rasemblant in a pdf file.
example: a photo in jpeg 47K bytes to 247 Kbytes made ​​in pdf
I am a beginner please m'escuser if I make large coding error. :oops:

Code:
                    XImage img1 = Pic1Resize;
                    XRect Rec = new XRect(0, 0, Pic1Resize.Width, Pic1Resize.Height);
                    gfx.DrawImage(img1, Rec);

Author:  Thomas Hoevel [ Wed Oct 19, 2011 8:18 am ]
Post subject:  Re: Format to jpeg in the Pdf

lolo818 wrote:
example: a photo in jpeg 47K bytes to 247 Kbytes made ​​in pdf
JPEG files are embedded into PDF without modification, so the PDF file will normally be larger than the JPEG file.
It shouldn't grow from 47 kB to 247 kB - but we'd need the JPEG and the PDF to tell where the 200 kB come from.

Author:  lolo818 [ Wed Oct 19, 2011 6:20 pm ]
Post subject:  Re: Format to jpeg in the Pdf

Good evening, I remade a test, but without success, the original image is always lighter than the generated pdf.
I put a picture and the PDF created with this code, is there no way to compress to ease the pdf file?
Thank you for your help.

Code:
                        PdfDocument document = new PdfDocument();
                        PdfPage page = document.AddPage();
                        page.Size = PageSize.A4;
                        XGraphics gfx = XGraphics.FromPdfPage(page);
                        page.Orientation = PageOrientation.Landscape;
                        XImage img1 = Pic1Resize;
                        XRect Rec = new XRect(0, 0, Pic1Resize.Width, Pic1Resize.Height);
                        gfx.DrawImage(img1, Rec);
                        document.Save(saveFileDialog1.FileName);


Attachments:
Pic1.JPG
Pic1.JPG [ 15.33 KiB | Viewed 8480 times ]
test.zip [71.94 KiB]
Downloaded 455 times

Author:  Thomas Hoevel [ Thu Oct 20, 2011 7:11 am ]
Post subject:  Re: Format to jpeg in the Pdf

lolo818 wrote:
I remade a test, but without success, the original image is always lighter than the generated pdf.

The image included in the PDF file is not a JPEG image.

Your code snippet doesn't show where Pic1Resize comes from. I guess it's not loaded from a JPEG file (maybe it's originally loaded from a JPEG file, but modified later on).

If your code resizes the image: save that image as JPEG (specifying the image quality you need) and load it again. Then the PDF file will contain a JPEG compressed image (much smaller).
Saving to a memory stream and loading from that memory stream should do (no file I/O involved).

I tried this code:
Code:
XImage image = XImage.FromFile(@"C:\Users\thho\Desktop\$$$\$$$PDFsharp\PicResize\pic1.JPG");
gfx.DrawImage(image, 0, 0, image.PixelWidth * 2, image.PixelHeight * 2);

The resulting PDF file was 15 kiB in size (15,108 bytes) while your JPEG image has 15,700 bytes. PDF file is smaller here!
I include the PDF.

Calling "XImage.FromGdiPlusImage(Image.FromStream(stream))" should do the trick.

Or add a new "FromStream" to the XImage class:
Code:
/// <summary>
/// Creates an image from the specified stream.
/// </summary>
public static XImage FromStream(Stream stream)
{
  return new XImage(stream);
}


Attachments:
HelloWorld_tempfile.zip [14.58 KiB]
Downloaded 468 times

Author:  lolo818 [ Thu Oct 20, 2011 9:15 pm ]
Post subject:  Re: Format to jpeg in the Pdf

Good evening, I actually was retouched bitmap after loading, I reformatted with a JPEG stream now everything is ok.
Thank you for your help and your patiently!
:D

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/