0

I'm saving a image but I need convert this image to pdf and save It. How can I do this?

Here is the code which I have used:

private void button3_Click(object sender, EventArgs e)
    {
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        saveFileDialog1.FileName = "image.bmp";
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {

            Bitmap bitmap = new Bitmap(this.Width, this.Height);
            this.DrawToBitmap(bitmap, this.ClientRectangle);
            using (var Stream = saveFileDialog1.OpenFile())
            {
                bitmap.Save(Stream, ImageFormat.Bmp);
            }

        }
}
Sjoerd
  • 74,049
  • 16
  • 131
  • 175
Ladessa
  • 985
  • 4
  • 24
  • 50
  • You can use iTextSharp. check [this](http://stackoverflow.com/questions/7115242/insert-an-image-in-pdf-using-itextsharp) – Mehmet Ataş Jan 28 '13 at 15:28
  • You need to use a specific library for that ! see PDFSharp and many others ! – HichemSeeSharp Jan 28 '13 at 15:28
  • 2
    Possible duplicate of http://stackoverflow.com/questions/1642280/jpg-to-pdf-convertor-in-c-sharp http://stackoverflow.com/questions/6775048/save-pdf-to-jpeg-using-c-sharp and so on – t3hn00b Jan 28 '13 at 15:29

3 Answers3

3

You'll need to use a PDF library, such as PDFSharp.

Here's some sample code to add an image to a PDF:

void DrawImage(XGraphics gfx, int number)
{
  BeginBox(gfx, number, "DrawImage (original)");

  XImage image = XImage.FromFile(jpegSamplePath);

  // Left position in point
  double x = (250 - image.PixelWidth * 72 / image.HorizontalResolution) / 2;
  gfx.DrawImage(image, x, 0);

  EndBox(gfx);
}
jrummell
  • 42,637
  • 17
  • 112
  • 171
  • well, event here "Don't repeat your self principle" should normally work. We need to re-factor questions and answers not duplicating them ! – HichemSeeSharp Jan 28 '13 at 15:48
  • Agreed. After posting this answer I saw the comment about dupes and voted to close :) – jrummell Jan 28 '13 at 15:55
  • `PdfDocument doc = new PdfDocument(); doc.Pages.Add(new PdfPage()); XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]); XImage img = XImage.FromFile(source); xgr.DrawImage(img, 0, 0); doc.Save(destinaton); doc.Close();` How do I to generate a XImage from `Bitmap bitmap = new Bitmap(this.Width, this.Height);` for example..? – Ladessa Jan 28 '13 at 16:07
  • 1
    Well, you could either draw to the PDF directly, or you could save your Bitmap to a file and use the path when calling `XImage.FromFile(source)`. – jrummell Jan 28 '13 at 16:14
0

I use iTextSharp to convert to PDF

Avitus
  • 15,640
  • 6
  • 43
  • 53
0

Pdf is a container format. So can't save a bitmap as a pdf, instead you have embed the bitmap in the pdf

iTextSharp is an perfect fit for this See Insert an Image in PDF using ITextSharp

Community
  • 1
  • 1
lboshuizen
  • 2,746
  • 17
  • 20