PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 2:09 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Mon Feb 27, 2017 1:47 pm 
Offline

Joined: Mon Feb 27, 2017 1:06 pm
Posts: 3
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


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 27, 2017 9:50 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 28, 2017 8:44 am 
Offline

Joined: Mon Feb 27, 2017 1:06 pm
Posts: 3
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


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 28, 2017 10:08 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 01, 2017 1:51 pm 
Offline

Joined: Mon Feb 27, 2017 1:06 pm
Posts: 3
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


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 01, 2017 1:58 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 123 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group