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

How to show an image from SQL Database on PDF?
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3546
Page 1 of 1

Author:  fatihbural [ Mon Feb 27, 2017 1:47 pm ]
Post subject:  How to show an image from SQL Database on PDF?

Dear All,

I have made a lot of search on the web but I couldn't find anything that can resolve my problem.

How can I get an image from database (SQL Server 2012) and show on PDF?

graph.DrawImage(XImage.FromFile(@"C:\Users\Fatih BURAL\Desktop\photos\sample.png"), 5, 735);

Above code works only when our software connected to local network and only if I could share "photos" folder on the network. When the user connected from away (home, cafes etc.) it does not show any images throws exceptions as expected.

I have also tried by loading image on the datagridview or pictureboxes to show with the coordinates below;

graph.DrawImage(XImage.FromFile(dataGridview1.Rows[0].Cells[0].Value.ToString()), 5, 735);

graph.DrawImage(XImage.FromStream(dataGridview1.Rows[0].Cells[0].Value.ToString()), 5, 735);

But above codes gave errors and I have made some search again and found nothing about this issue. Could anyone help me with this issue?

Best Regards

Fatih

Author:  TH-Soft [ Mon Feb 27, 2017 9:50 pm ]
Post subject:  Re: How to show an image from SQL Database on PDF?

Hi!

Transfer the images to the customer computer as a byte[].
Create a MemoryStream from that byte[] and use XImage.FromStream() to create the image.

I don't know in which formats you can get images from the DataGridview.

Author:  fatihbural [ Tue Feb 28, 2017 8:44 am ]
Post subject:  Re: How to show an image from SQL Database on PDF?

Hello Thomas,

Now it's working with your help. Thank you very much. I'm posting my code down below to help others if someone got into same trouble;

Code:
           byte[] imageData = (byte[])dataGridView1.Rows[0].Cells[0].Value;

            XImage newImage;
              using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
            {
                ms.Write(imageData, 0, imageData.Length);
                newImage = XImage.FromStream(ms);
            }
            graph.DrawImage(newImage, 5, 735);

Best Regards

Fatih BURAL

Author:  Thomas Hoevel [ Tue Feb 28, 2017 10:08 am ]
Post subject:  Re: How to show an image from SQL Database on PDF?

Hi!
Thanks for sharing your code.
Theoretically you can simplify your code like this:
Code:
           byte[] imageData = (byte[])dataGridView1.Rows[0].Cells[0].Value;

            using (MemoryStream ms = new MemoryStream(imageData))
            {
                var newImage = XImage.FromStream(ms);
                graph.DrawImage(newImage, 5, 735);
            }

No need to write to the stream if you initialize it with a filled byte[].
I think having the DrawImage in the "using" block may prevent certain problems with some image formats.

Author:  fatihbural [ Wed Mar 01, 2017 1:51 pm ]
Post subject:  Re: How to show an image from SQL Database on PDF?

Hello Thomas,

Thank you for your correction. I have changed my code into yours now. It makes me happy to see it's working :)
So I will have another question about creating new pages while ongoing rendering with continuous data. But I am not sure if should create new topic?

Best Regards

Fatih BURAL

Author:  Thomas Hoevel [ Wed Mar 01, 2017 1:58 pm ]
Post subject:  Re: How to show an image from SQL Database on PDF?

fatihbural wrote:
But I am not sure if should create new topic?
That depends.
PDFsharp: Search for "AddPage", and maybe also "pagebreak".
MigraDoc: Search for "AddPagebreak", "AddSection", and maybe also "pagebreak".

I think it cannot harm to start a new thread if the question does not relate to any code shown here. Maybe you can ask in a thread that Search found for you if that thread is somewhat helpful, but not exactly what you need.

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