D
Detlef Hüttenbach
Whereas loading tiffs and pngs from file into an Image WebControl work, the
images aren't shown when loaded from streams:
In a WebForm, the image's property "ImageUrl" is set to a handler, and this
handler gets the image from a database.
Please check the various image formats. (The images are stored in a SQL
database as Image type.)
I post you a demo HTTP handler below.
I guess, it's the ViewState which messes up (in Windows Forms I don't have
any problems to load the images from stream.)
Please, have a look at it.
Best wishes
Detlef
---------------Demo Handler Code --------------------------
<%@ 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; }
}
}
images aren't shown when loaded from streams:
In a WebForm, the image's property "ImageUrl" is set to a handler, and this
handler gets the image from a database.
Please check the various image formats. (The images are stored in a SQL
database as Image type.)
I post you a demo HTTP handler below.
I guess, it's the ViewState which messes up (in Windows Forms I don't have
any problems to load the images from stream.)
Please, have a look at it.
Best wishes
Detlef
---------------Demo Handler Code --------------------------
<%@ 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; }
}
}