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

Using an image from an URL
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1178
Page 1 of 1

Author:  yonex68 [ Tue May 11, 2010 12:54 pm ]
Post subject:  Using an image from an URL

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!

Author:  Thomas Hoevel [ Tue May 11, 2010 2:08 pm ]
Post subject:  Re: Using an image from an URL

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:

Author:  Soldier-B [ Wed May 19, 2010 5:01 pm ]
Post subject:  Re: Using an image from an URL

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);
}

Author:  ericnord [ Mon Jul 12, 2010 5:47 pm ]
Post subject:  Re: Using an image from an URL

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.

Author:  ericnord [ Mon Jul 12, 2010 7:27 pm ]
Post subject:  Re: Using an image from an URL

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?

Author:  ericnord [ Thu Jul 15, 2010 5:57 pm ]
Post subject:  Re: Using an image from an URL

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.

Author:  rfransdonk [ Sat Jul 23, 2011 10:39 pm ]
Post subject:  Re: Using an image from an URL

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

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