PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu May 16, 2024 1:52 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Tue May 11, 2010 12:54 pm 
Offline

Joined: Tue May 11, 2010 12:50 pm
Posts: 1
Hello,

I'm trying to insert an image from an URL address, but an error occured: "path's format not supported". Is it really impossible to do that?
Here is the code:

const string img_voda = "https://etc.../Images/3gbutton";
XImage image = XImage.FromFile(img_voda);

Thank you for your help!


Top
 Profile  
Reply with quote  
PostPosted: Tue May 11, 2010 2:08 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3097
Location: Cologne, Germany
Hi!
yonex68 wrote:
Is it really impossible to do that?

An URL is not a filename and this is not yet implemented (and won't be implemented in the near future).
But it's not impossible, so feel free to make the necessary changes ... :wink:

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed May 19, 2010 5:01 pm 
Offline
User avatar

Joined: Tue Oct 14, 2008 6:15 pm
Posts: 32
Location: USA
You could use the HttpWebRequest and HttpWebResponse classes in the System.Net namespace to download the image data, then create a bitmap object from that data, and then finally create an ximage from your bitmap.

Here's a quick sample:
Code:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"http://forum.pdfsharp.net/styles/prosilver/imageset/site_logo.gif");
HttpWebResponse res = (HttpWebResponse)req.GetResponse();

if (res.StatusCode == HttpStatusCode.OK)
{
    Bitmap bmp = (Bitmap)Image.FromStream(res.GetResponseStream(), true, true);
    XImage image = XImage.FromGdiPlusImage(bmp);
}


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 12, 2010 5:47 pm 
Offline

Joined: Mon Jul 12, 2010 5:41 pm
Posts: 3
I'm using PdfSharp to create pdfs on the fly and I need to include images from URL's most likely. I'm trying to implement this, but in WPF since I'm working in azure and can't use GDI+. I'm pretty newbish with C#, is it possible to use the method FromBitmapSource() to accomplish this? Could someone walk me through how I might do that?

Thanks.


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 12, 2010 7:27 pm 
Offline

Joined: Mon Jul 12, 2010 5:41 pm
Posts: 3
So I'm still working on it and so far I've gotten this:
Code:
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri(@"someURL");
                bi.EndInit();
                using (XImage image = XImage.FromBitmapSource(bi))
                {
                    imageScaledWidth = image.PixelWidth / 4;
                    imageScaledHeight = image.PixelHeight / 4;
                    gfx.DrawImage(image, 80, 190 + addedEluentLength, imageScaledWidth, imageScaledHeight);
                }

But it's definitely not working (no image gets displayed). I'm not sure if the problem is with BitmapImage or with the FromBitmapSource() method. Any ideas?


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 15, 2010 5:57 pm 
Offline

Joined: Mon Jul 12, 2010 5:41 pm
Posts: 3
So I'm sure other people are curious about this because I can't be the only one who was trying to do it. But I figured it out, from tweaking some sample code I found online. Maybe someone could add this as a method into the code because it really should be something that is in pdfSharp by default:

Code:
           
            var context = ControllerContext.HttpContext;
            string tempPath = "" + context.Request.Url;
            string path = tempPath.Replace("" + context.Request.Path, "");
            const int BYTESTOREAD = 10000;
            WebRequest myRequest = WebRequest.Create(path + "/Content/Images/pdf/logoTop.png");
            WebResponse myResponse = myRequest.GetResponse();
            Stream ReceiveStream = myResponse.GetResponseStream();
            BinaryReader br = new BinaryReader(ReceiveStream);
            MemoryStream memstream = new MemoryStream();
            byte[] bytebuffer = new byte[BYTESTOREAD];
            int BytesRead = br.Read(bytebuffer, 0, BYTESTOREAD);
            while (BytesRead > 0)
            {
                memstream.Write(bytebuffer, 0, BytesRead);
                BytesRead = br.Read(bytebuffer, 0, BYTESTOREAD);
            }
            BitmapImage logo = new BitmapImage();
            logo.BeginInit();
            logo.StreamSource = memstream;
            logo.EndInit();
            using (XImage imageLogoTop = XImage.FromBitmapSource(logo))
            {
                imageScaledWidth = imageLogoTop.PixelWidth / 4;
                imageScaledHeight = imageLogoTop.PixelHeight / 4;
                gfx.DrawImage(imageLogoTop, 465, 20, imageScaledWidth, imageScaledHeight);
            }


It actually ended up being a lot more complicated than I expected, but it works.


Top
 Profile  
Reply with quote  
PostPosted: Sat Jul 23, 2011 10:39 pm 
Offline

Joined: Sat Jul 23, 2011 10:26 pm
Posts: 1
So if you want an XImage.FromURI method to use like this (example):

XImage image = XImage.FromURI("http://a.fsdn.com/con/icons/pd/pdfsharp@sf.net/PDFsharp-80x80.png");
gfx.DrawImage(image, 0, 0);

Just add the following code to XImage.cs:

/// <summary>
/// Creates an image from the specified URI.
/// </summary>
/// <param name="uri">The URI to a BMP, PNG, GIF, JPEG, TIFF, or PDF file.</param>
public static XImage FromURI(string uri)
{
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
webRequest.AllowWriteStreamBuffering = true;
WebResponse webResponse = webRequest.GetResponse();
Image image = Image.FromStream(webResponse.GetResponseStream());
webResponse.Close();
return new XImage(image);
}

have fun,
Robert


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 17 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