locked
GDI+; Create Bitmap object from URL, is it possible? RRS feed

  • Question

  • User1230978511 posted
    I've noticed many instances where I'd like to create an object from a URL (an image, XML document, XSL document, etc) and have not been able to do so.  Most of these objects require the resource to be local to the filesystem (i.e; http://msdn.microsoft.com/en-us/library/0cbhe98f.aspx).  Can anyone help me determine whether it's possible to create an instance of Bitmap from a URL resource?

    Also, given an edited Bitmap object, I'm going to want to set this object to a ImageButton on my ASP.NET page.  However, the ImageButton object only has support for an "ImageUrl" instead of an image object.

    Any advice here is greatly appreciated.
    Tuesday, May 9, 2006 3:27 PM

All replies

  • User1230978511 posted
    WebClient webClient = new WebClient();
    Bitmap oCanvas = new Bitmap(webClient.OpenRead(this.MapImageUrl));

    After a bunch of searching I randomly came across this, which seems to work nicely.  Now I just need to figure out how to display the altered Bitmap in my ImageButton server control without saving it to disk.
    Tuesday, May 9, 2006 4:11 PM
  • User1119954915 posted

    I'd suggest posting in the GDI+ forum

    http://forums.asp.net/150/ShowForum.aspx

    lots of people in there that know that set of classes inside and out

    Tuesday, May 9, 2006 4:23 PM
  • User-2041805088 posted

    I just moved this thread from the Getting Started forum to the System.Drawing/GDI+ forum.

    Ryan

    Tuesday, May 9, 2006 4:51 PM
  • User1230978511 posted
    Thanks for the move!

    Has anyone taken a Bitmap object from memory and displayed it within an ImageButton server control without stamping the image to the filesystem?
    Wednesday, May 10, 2006 11:03 AM
  • User1230978511 posted
    Anyone?
    Tuesday, May 16, 2006 2:09 PM
  • User-1624456073 posted
    What you need, is a custom http handler that can serve the Bitmap for you.
    You can use an .ashx file for this, or compile it in an assembly and register it in the web.config file if yo u want a custom handler mapping (for example, have it handle all .myextention requests). In either case you'll be implementing the IHttpHandler interface. A very simplistic http handler capable of writing a Bitmap to the response stream would look something like:

    public class ImageHandler : IHttpHandler
    {
        public bool IsReusable
        {
           get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
           using(Image image = GetBitmap(context))
           {
               context.Response.ContentType = "image/jpeg";
               image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
           }
        }

        public Bitmap GetBitmap(HttpContext context)
        {
           // load your image here. use the context to
           // get at the url/querystring to determine what image to load;
           // f.ex MyHandler.ashx?Image=someImageIdentifier
        }
    }
    Tuesday, May 16, 2006 4:31 PM
  • User1230978511 posted
    I think I understand what you're saying.  So any request for a given extension (say, .myBitmap) would be intercepted by my custom HttpHandler and processed as I declare it.

    Let me explain my flow a little better to see if this solution fits.  First, a user might zoom in and view an image (jpg image).  Then they might select to create a polygon.  So after they change their mode to "Create Polygon" they'll select a point on the screen.  That's when I had been getting the last image created (that same jpg) and using GDI+ to add a big red dot to that image.  I'd then stamp the edited image and set the src of the mapImage control to the edited image.  This point process would repeat until they told me they were done building the polygon.

    Given this flow, is it still possible to intercept the request only when it's for the point addition piece or will I need to check each request since the mapImage control serves both "normal" jpg images and the edited BitMap images?
    Tuesday, May 16, 2006 8:32 PM
  • User-1624456073 posted
    I think I understand what you're saying.  So any request for a given extension (say, .myBitmap) would be intercepted by my custom HttpHandler and processed as I declare it.

    That's right :)

    Given this flow, is it still possible to intercept the request only when it's for the point addition piece or will I need to check each request since the mapImage control serves both "normal" jpg images and the edited BitMap images?

    If you register the handler with the .myBitmap extention, it will intercept all those requests, but you could certainly implement the handler so that it serves the unmodified original jpeg unless the user is in the 'create polygon' state, as you describe.

    You could have the src set to the static "myimage.jpg" file originally however, and when your user goes into editing mode, change the url to "myimage.myBitmap", which would then let the handler load the image into a Bitmap, and you could do the editing etc and serve it dynamically. This would probably be the most efficient solution.

    Wednesday, May 17, 2006 6:42 AM