Pass IMG Number

  • Thread starter Thread starter Thom Little
  • Start date Start date
T

Thom Little

I need a C# (aspx or asmx) that I pass a string and it returns the string
formatted as an image. It would be something like ...

<img src="http://www.domain.com/sample/123456">

.... where 123456 would be rendered as a graphics image.

Can you point me towards an an example/sample?

_______________________________________________
Thom Little www.tlanet.net Thom Little Associates, Ltd.
 
Image img = Image.FromFile(".\\blank.jpg");
Graphics g = Graphics.FromImage(img);
try
{
g.DrawString("Any string", new Font("Arial", 14), new
SolidBrush(Color.Black), 0, 0);
this.pictureBox1.Image = img;
}
finally
{
if (g != null)
g.Dispose();
}

return;


The concept is similar whether you're on windows forms or ASP.Net (as you
seem to desire). Simply load some blank image from disk, write onto it using
the Graphics object, and then save it back to disk or serialize it as a byte
stream to the client, etc.
 
Thank you for the help.

The aspect I have yet to see is passing the data between the calling page
and the processing page. Clearly the call looks lie ...

<img src="http://www.domain.com/tool/test.aspx?123456">

.... where apparetnly 123456 shows up as a normal Request.QueryString?

How is the bitmap (graphics) response defined in the C# processing page?

Is there a reference you could point me at so that I don't waste your time?

_______________________________________________
Thom Little www.tlanet.net Thom Little Associates, Ltd.
 
There are a myriad of different ways to get it working.

You could simply take this new bitmap you've created (using the code I gave
you), save it to disk, and set the image url property of some ASP.net image
control to point to the path of the image you saved on disk, or you could
even create a custom httphandler that will handle a request for an image and
return back to the browser the headers, bits etc. necessary to supply an
image (content-type, etc)

As far as a sample on the net, I'm not sure of one. I don't have the
bandwidth right now to write up more of a sample than the one I already did
nor really look for a more in depth one.
 
I (finally) found the good example in the Visual Studio 2005 Help file.

Thanks for your help.

_______________________________________________
Thom Little www.tlanet.net Thom Little Associates, Ltd.
 
I'm a "non graphics" person (it shows) and you helped me get off the dime.

I can make a request on an IMG statement and get a newly constructed .jpg
returned.

My curious current problems are ...
How to set the background to transparent with Clear
How to set the font color to limegreen with DrawString

_______________________________________________
Thom Little www.tlanet.net Thom Little Associates, Ltd.
 
Bitmap bmp = new Bitmap(128, 128);

Graphics g = Graphics.FromImage(bmp);
try
{
g.DrawString("Any string", new Font("Arial", 14), new
SolidBrush(Color.LimeGreen), 0, 0);
this.pictureBox1.Image = bmp;
}
finally
{
if (g != null)
g.Dispose();
}

Try that, it should work for you. Create your bitmap in memory instead of
loading something from disk; then just draw onto it with a limegreen brush
like above. Don't specify any background, etc and no color data will be
specified elsewhere; this will give that transparent effect the way, I
think, you need it.

Please make sure to call Dispose on the Graphics object.
 
What I currently have (with your help) ...

Response.ContentType = "image/jpeg" ;
Response.Clear( );
Response.BufferOutput = true ;
Font font = new Font( "Arial", 11, FontStyle.Bold );
Brush brush = new SolidBrush( Color.LimeGreen, 0, 0 );
int height = 20 ;
int width = 80 ;
Bitmap bmp = new Bitmap( width, height, PixelFormat.Format24bppRgb );
Graphics g = Graphics.FromImage( bmp );
g.DrawString( Request.QueryString.ToString( ), font, brush, new PointF( 8, 2
) );
bmp.Save( Response.OutputStream, ImageFormat.Jpeg );
g.Dispose( );
bmp.Dispose( );
Response.Flush( );

.... this returns an image that is limegreen on a black background to the
calling page.

_______________________________________________
Thom Little www.tlanet.net Thom Little Associates, Ltd.
 
I'm not at my computer at the moment so I cannot tell for sure, but
what happens if you switch the file format from JPEG to GIF? JPEG
doesn't support any alpha layer; which is what you need to support in
order to have any transparent element in your image.

It's been a while since I've done anything in graphics programming, so
I'm a bit rusty....sorry....

Ben
 
I blundered on that one already and changed the transmission options to GIF.
It still insists on a black background.

I also tried adding ...
g.Clear( Color.Transparent );
.... and that also did not help.

(There must be a "trick" that only the worthy get to know.)

_______________________________________________
Thom Little www.tlanet.net Thom Little Associates, Ltd.
 
Sorry about the massive # of posts. Google hung when I tried to submit the
post and somehow posted too many times.
 
I appreciate your responses and I am shlogging my way through the article.
I don't see how the memory stream emitted from the method gets sent back to
the calling page. Something like ...

private void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.BufferOutput = true ;
Response.ContentType = "image/gif" ;
MemoryStream OutputStream = DrawTransparentGif( "Hello GIF", Color.Maroon,
100, 100 );
Response.Save(Response.OutputStream, ImageFormat.Gif );
Response.Flush( );
}

.... but I honestly am lost.

(First graphics application.)

_______________________________________________
Thom Little www.tlanet.net Thom Little Associates, Ltd.
 
Some Bits!

That is exactly what I needed to invoke the solution that you provided that
is exactly what I need to solve my problem.

Thank you very much.

_______________________________________________
Thom Little www.tlanet.net Thom Little Associates, Ltd.
 
Here's a another dumb question ...

The technique you provided works perfectly when the foreground (text) color
is anything other than black.

What has to be done to support black text on a transparent background?

_______________________________________________
Thom Little www.tlanet.net Thom Little Associates, Ltd.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

HTML AJAX to ASP.NET 2.0 3
DAtagrid for Flat File 3
SMTP and POP3 Healthy 3
Read ASP 3 Session Data 5
C GetParentFolderName 3
Network Health 5
HTML AJAX to DOT.NET in Different Domain 9
Organization 3

Back
Top