Problems with Image.FromStream()

  • Thread starter Detlef Huettenbach
  • Start date
D

Detlef Huettenbach

I was trying to convert a Windows Forms prototype
application to an ASP.NET solution that makes use of
loading data streams into the Image Web/Windows control.
For WinForms no problem. However in ASP.NET, the image
control does not display tiffs,pngs,.. still it does
display gif-streams. Memory stream writing into a gif-
stream of the tiff and other streams likewise fails,
although the streams seem to be filled correctly.
On top of that, when I save the streams to a file and
open them by Image.FromFile, the images are displayed!

So, what's on? - Is there a more convenient work around?

Thanks for response,
Detlef
 
K

Kevin Spencer

ASP.Net is a technology which creates HTML documents for the most part. An
image control displays an image using HTML, which means that the image is
not IN the page, but a URL to the image appears in an image tag in the page.
All the files used to display the page are requested separately by the
browser and assembled by the browser in the browser window. Therefore, in
order to display an image from a stream in a page you have to include a URL
in that page that points to another ASP.Net page that sets the
Response.ContentType property to "image/jpg" (or whatever image format
you're using), and saves the image stream to the Response.OutputStream of
the page. In other words, the second page acts just like an image.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
D

Detlef Hüttenbach

Thank you very much for your answer, and yes, I set the images URL,
namely to an HttpHandler, and in that handler the content type is set to
image/tiff, /png etc.

Unfortunately, right now at home, I can't look into the code at work.
I'll do it tomorrow, will see whether it will work if I using yet
another aspx page with an image control as an intermeditate, and will
post the result.

Thanks for your answer
Detlef
 
D

Detlef Hüttenbach

Hi Kevin,

Thank you for your kind answer.
Yes, URL is set on image control, pointing to
an HTTP handler, where the images are retrieved
and the Response Content Type is set to the
appropriate image/tiff ... value.

Unfortunately, here, at home, I can't look
into the app at work. So, tomorrow, I'll
give it another try with yet another aspx
image page as intermediary and will post
the result + some code.

Thanks again for your respone
Detlef
 
G

Guest

Hi all, and Kevin in particular,

here's a source for the HTTP handler that picks
the image files from a database (this case *.png) and
handles it over to an image on a Webform with
Image.ImageURL="...ashx" (although you could rightaway
also set the src-attribute of an <img/>-element.
Figure around: the pics wont show up on the WebForm.
What's the cause? - The ViewState?

Thanks for response
Detlef
--
<%@ WebHandler Language="C#" Class="PicViewer" %>

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.ComponentModel;

public class PicViewer : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
System.Data.SqlClient.SqlConnection
sqlConnInvoice;
Graphics g;
Bitmap bmp;
MemoryStream bmpStream;
Image _img;
MemoryStream memStream;
byte[] _picBytes;

//Get PicBytes, in this case Png Data,
where PngPic is a
//SQL-DataField of SQL format Image
conn = new
System.Data.SqlClient.SqlConnection();
conn.ConnectionString = "...yerkyerk...";
sqlConnInvoice.Open();
System.Data.SqlClient.SqlCommand
_cmdLoadPicture =
new
System.Data.SqlClient.SqlCommand("select PngPic from
PngPictures where PicID=52531;",conn);
SqlDataReader drReader =
_cmdLoadPicture.ExecuteReader();
drReader.Read();
_picBytes=(byte[])drReader.GetSqlBinary(0);
//Push data to Memory Stream
memStream = new MemoryStream();
memStream.Write
(_picBytes,0,_picBytes.Length);

//Commented to follow a more general route:
//Make a Bitmap, print image into it and
save
//it into another standard format, here
*.gif,
//but could be any other
/*
//Construct image to push into response
stream
_img=Image.FromStream(memStream,true);
*/

//Read image size
float _height = Image.FromStream
(memStream).PhysicalDimension.Height;
float _width = Image.FromStream
(memStream).PhysicalDimension.Width;

//new Bitmap - adapt size if necessary:
bmp = new Bitmap(Image.FromStream
(memStream), new Size((int)_width, (int)_height));
g = Graphics.FromImage (bmp);

//Draw on Bitmap
g.DrawImage(_img,0,0);
memStream.Close();
conn.Close();

/*
context.Response.ContentType = "image/gif";
//Save _img into Reponse stream - try
other format
_img.Save
(context.Response.OutputStream,ImageFormat.Gif);
*/

//Save bmp into Reponse stream
context.Response.ContentType="image/gif";
bmp.Save(context.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Gif);
bmp.Dispose();
g.Dispose();
}

public bool IsReusable
{
get { return true; }
}
}
//----------
 

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

Top