RE: How can I get a bitmap into a WebControl?



Hi Dave,

Based on my understanding, your situation is:
1) You currently have a WinForm control that can show image which is
dynamically generated on the fly;
2) You want to create a Web control that can also do the same thing;
3) Also, you don't want to create a WebForm to generate the image;
If I've misunderstood anything, please feel free to post here.

I suppose you already have working code that generates the image on the fly
which I also assume it's something like a Bitmap object. A Bitmap object
can save to a MemoryStream which again can write to ASP.NET Response
directly; for example:

Bitmap bitmap = <your code to generate the image on the fly>;
MemoryStream ms = new MemoryStream();
Response.Clear();
Response.ContentType = "image/jpeg";
bitmap.Save(ms, ImageFormat.Jpeg);
ms.WriteTo(Response.OutputStream);

Next, we need to create a ASP.NET custom control which takes some
properties and generate a resource url which uses a custom http handler to
generate the image:

public class MyDynamicImage: System.Web.UI.WebControls.Image
{

protected override void OnPreRender(EventArgs e)
{
...
ImageUrl = String.Format("~/__ImageGrabber.axd?p1={0}&p2={1}",
...);
}
}

The __ImageGrabber.axd is a custom http handler which is configured in
web.config like this:

<configuration>
<system.web>
<httpHandlers>
<add verb="GET" path="__ImageGrabber.axd"
type="MsdnMag.ImageGrabber" />
</httpHandlers>
</system.web>
</configuration>

The type MsdnMag.ImageGrabber is derived from IHttpHandler:

namespace MsdnMag
{
public class ImageGrabber : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
...
}
}
}

Then in your ASP.NET WebForm, just use the MyDynamicImage control and pass
required properties to generate different images.

For more complete code listing, please refer to following MSDN Magazine
article:

#Wicked Code: Power Programming Tips for ASP.NET 2.0
http://msdn.microsoft.com/msdnmag/issues/05/06/WickedCode/

Search the section named "Fetching Images from DataBases."

I hope this helps. Please feel free to post here if anything is unclear.

Sincerely,
Walter Wang (wawang@xxxxxxxxxxxxxxxxxxxx, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

.



Relevant Pages

  • Re: Web Form/SQL Table Structure
    ... So the Repeater tool output would look something like this: ... How would I call the correct control for the question (DropDownList, ... Microsoft MSDN Online Support Lead ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: ContextMenuStrip and PreviewKeyDown
    ... Win32 control message model and input model. ... System.Windows.Forms.ContextMenuStrip class to "MyContextMenuStrip" class ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: ObjectList Data-Binding to a List
    ... All I want is a control that allows me to manually build HTML ... Thanks for getting back to me about all these posts. ... Microsoft MSDN Online Support Lead ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: DropDownList Input
    ... The control will then be added to the toolbox. ... "Nathan Sokalski" wrote: ... Microsoft MSDN Online Support Lead ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: trouble decoding managed call stack causing kernel complaint
    ... verbose symbol loading. ... you may use spy++ to find out that control. ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.win32.programmer.kernel)

Loading