473,508 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

**BUG** Loading Images from Stream into aspx-Webform

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.Heig ht;

float _width = Image.FromStream(memStream).PhysicalDimension.Widt h;

//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,ImageForma t.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; }

}

}


Nov 18 '05 #1
11 5266
Replacement error:

Please change the line
System.Data.SqlClient.SqlConnection sqlConnInvoice;
into
System.Data.SqlClient.SqlConnection conn;

Thanks
Detlef
Nov 18 '05 #2
Sorry, there were still two replacement errors, so that's
the handler:
---------Handler----------
<%@ 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 void ProcessRequest(HttpContext context)
{
System.Data.SqlClient.SqlConnection conn;
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...";
conn.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.Heig ht;
float _width = Image.FromStream(memStream).PhysicalDimension.Widt h;

//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(bmp,0,0);
memStream.Close();
conn.Close();

/*
context.Response.ContentType = "image/gif";
//Save _img into Reponse stream - try other format
_img.Save(context.Response.OutputStream,ImageForma t.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; }
}
}
Nov 18 '05 #3
First of all, I'd suggest you make a simple test case (that anyone interested in helping you can compile and run on their machines) that shows the same problem rather than giving us a boot load of code can't be run anywhere else :). This is just from years of being involved in newsgroups and helping others and being helped.

Secondly, have you tried writing to the OutputStream directly (instead of Save)

System.IO.Stream stream = Response.OutputStream;

stream can now be used

I should say that I have been doing this kind of thing but haven't encountered any problems. Are you saying this problem exists only for tiff and png and not for others?
--
Shiv R. Kumar
http://www.matlus.com
Nov 18 '05 #4
First of all, I'd suggest you make a simple test case (that anyone interested in helping you can compile and run on their machines) that shows the same problem rather than giving us a boot load of code can't be run anywhere else :). This is just from years of being involved in newsgroups and helping others and being helped.

Secondly, have you tried writing to the OutputStream directly (instead of Save)

System.IO.Stream stream = Response.OutputStream;

stream can now be used

I should say that I have been doing this kind of thing but haven't encountered any problems. Are you saying this problem exists only for tiff and png and not for others?
--
Shiv R. Kumar
http://www.matlus.com
Nov 18 '05 #5
okay i have used handler to fetch and output images from db... and i did
encounter a very stranger error
if i directly load the image onto a stream.. read it to a Image object and
use Image.Save... it used to output error which was invalid parameters of
something like that.
but on the same image i would essentially use img.GetThumbnail (cant
remember the full name) and i could output the resulting imgThumb using
imgThumb.Save(Response.OutputStream,Imagetyupe...)

to get over it loading the byte[] into stream and writing it to output
stream.. i just passed additional param saying whether i needed thumbnail...
if not then i just use Response.BinaryWrite(bytearray)
if its thumbnail then create the thumbnail and then
imgThumb.Save(Response.OutputStream, ImageType)
if posting my code would help then give me a shout..

--

Regards,

HD
"Detlef Hüttenbach" <d.***********@netcologne.de> wrote in message
news:bu**********@newsreader2.netcologne.de...
Sorry, there were still two replacement errors, so that's
the handler:
---------Handler----------
<%@ 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 void ProcessRequest(HttpContext context)
{
System.Data.SqlClient.SqlConnection conn;
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...";
conn.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.Heig ht;
float _width = Image.FromStream(memStream).PhysicalDimension.Widt h;

//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(bmp,0,0);
memStream.Close();
conn.Close();

/*
context.Response.ContentType = "image/gif";
//Save _img into Reponse stream - try other format
_img.Save(context.Response.OutputStream,ImageForma t.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; }
}
}

Nov 18 '05 #6
Hi Shiv,

thanks for your answer. I posted a reply this morning from work, but that didn't show up until now fro my home site.
Yes, I do have a tiny demo here at home, and the large db at work I can't even script let alone publish even for parts.
What do you mean by "save indirectly"? I posted this morning a snippet by which that stream is wrapped by yet another one before it is pushed into the Response OutputStream. That doesn't work either. If you mean by "save" to save the stream onto the disk and then to open the file: yes, this does work, but that's not what I wanted.
As to your question whether the problem only held for tiffs and pngs: I could swear I had some code working for gifs at work, but today did not find it. Right here at home, the code that I here, does to my dismay not work for gifs either.
Hermit above kindly pointed out that the Thumbnail could be a great option. I'll try that. If not this night, then tomorrow and keep you informed.

Thanks for your input
Detlef

"Shiv Kumar" <sh***@erolsnoooospaaaam.com> schrieb im Newsbeitrag news:uT****************@TK2MSFTNGP11.phx.gbl...
First of all, I'd suggest you make a simple test case (that anyone interested in helping you can compile and run on their machines) that shows the same problem rather than giving us a boot load of code can't be run anywhere else :). This is just from years of being involved in newsgroups and helping others and being helped.

Secondly, have you tried writing to the OutputStream directly (instead of Save)

System.IO.Stream stream = Response.OutputStream;

stream can now be used

I should say that I have been doing this kind of thing but haven't encountered any problems. Are you saying this problem exists only for tiff and png and not for others?
--
Shiv R. Kumar
http://www.matlus.com
Nov 18 '05 #7
Hi Hermit,

thank you for your great response. Thumbnails are indeed what I didn't
try out and a great idea to try!
If not this night, I'll try out tomorrow and post you the outcome.

Thanks again
Detlef
Nov 18 '05 #8
Hi Hermit,

Thumbnails didn't work either.
Used the code below.

Thanks for response
Detlef
---Code:---

SqlDataReader drReader = _cmdLoadPicture.ExecuteReader();
drReader.Read();
_picBytes=(byte[])drReader.GetSqlBinary(0);
memStream = new MemoryStream();
memStream.Write(_picBytes,0,_picBytes.Length);

_img=Image.FromStream(memStream,true);

_height = Image.FromStream(memStream).PhysicalDimension.Heig ht;
_width = Image.FromStream(memStream).PhysicalDimension.Widt h;

bmp = new Bitmap(Image.FromStream(memStream), new Size((int)_width,
(int)_height));
g = Graphics.FromImage (bmp);

Image.GetThumbnailImageAbort _cb =
new Image.GetThumbnailImageAbort(ThumbnailCallback);

_Thumb = _img.GetThumbnailImage((int)_width, (int)_height, _cb,
IntPtr.Zero);
g.DrawImage(_Thumb, 0, 0);

memStream.Close();
conn.Close();
context.Response.ContentType = "image/gif";

bmp.Save(context.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Gif);
bmp.Dispose();
g.Dispose();
Nov 18 '05 #9
Hi Shiv,

Thumbnails didn't work either.
(see posting under hermit above.)

Thanks
Detlef

Nov 18 '05 #10
Your code is not really the best code to use :)

For starters I should say that I have done what you are attempting to do and
it works as expected. I use jpeg and gif image formats (have no need for
png).

Unless I've missed something in your code, all you're doing is getting a
byte[] from your database and then making a thumbnail out of your byte[].

Is that correct? If so then given a byte[] all you need to do is:

System.Drawing.Image img = System.Drawing.Image.FromStream(new
MemoryStream(bytes));
System.Drawing.Image thumbnail;

System.IntPtr ptr = new IntPtr();

thumbnail = img.GetThumbnailImage(width, height, null, ptr);

//stream is Response.OutputStream

thumbnail.Save(stream, img.RawFormat);

thumbnail.Dispose();

img.Dispose();

--
Shiv R. Kumar
http://www.matlus.com
Nov 18 '05 #11
Hi Shiv,
"Shiv Kumar" <sh***@erolsnoooospaaaam.com> schrieb im Newsbeitrag
news:eB*************@TK2MSFTNGP10.phx.gbl...
Your code is not really the best code to use :) I gave up writing the best clean code, when I need to change it all the time
just to see that it works.
Unless I've missed something in your code, all you're doing is getting a
byte[] from your database and then making a thumbnail out of your byte[].
Is that correct? Yes. But: how, when if: I want to get the code work!
If so then given a byte[] all you need to do is:
System.Drawing.Image img = System.Drawing.Image.FromStream(new
MemoryStream(bytes));
System.Drawing.Image thumbnail;

System.IntPtr ptr = new IntPtr();

thumbnail = img.GetThumbnailImage(width, height, null, ptr);

//stream is Response.OutputStream

thumbnail.Save(stream, img.RawFormat);

thumbnail.Dispose();

img.Dispose();

Did that: does not work. There could still be some hope when picking certain
palettes and trying out.. But the fact that ViewState on the client side
stores a 2 line
hash for images of 100-500k doesn't convince me that piddling with palettes
will be useful doing.
If that's not a Framework bug, at least MS should publish about that
curiosity and give us
a workaround. Something strange happens to GDI+ within ASP.NET.

Thanks

Detlef
Nov 18 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2393
by: James Lang | last post by:
Hi I am trying to display in an aspx page an image from the employees table in the sql server 2000 Northhwind database "Select photo from employees where employeeid = 1"
1
1743
by: Detlef Hüttenbach | last post by:
There is a bug within the Handler factory that messes up the streams loaded from memory. Workaround: Don't use the HandlerFactory, i.e.: ashx-handlers when you need to print images from streams...
1
2865
by: Alec MacLean | last post by:
Hi, I'm trying to write a set of charts (as bitmap objects) out to a memory stream so that a variable number of charts can be displayed using just one html <img> tag in a holder page. I have...
3
3747
by: Richard | last post by:
I have an ASP.NET webapp that generates a shipping label for a customer. When the user clicks on the label preview gif or on a "Print..." submit button, the app will open another window with a...
7
6307
by: gemel | last post by:
I am developing an application that uses SQL 2000 as my source of images. I have successfully created the code to load the images onto the SQL Server and also to retrieve the images into a dataset....
5
5016
by: Peter Lapic | last post by:
I have to create a image web service that when it receives an imageid parameter it will return a gif image from a file that has been stored on the server. The client will be an asp.net web page...
7
2047
by: Sirplaya | last post by:
I am retrieving images that I stored in SQL Server on my web pages in C#. I have no problem with the images displaying, however, I am trying to wrap the image with an <A HREF ..." and each time I...
5
2298
by: =?Utf-8?B?VW1lc2huYXRo?= | last post by:
Hi, How can I load a image stored in SQL server database in ASP.NET page. I have used Response.BinaryWrite but it loads in a new page .I want to load in a part of my aspx page or in a control...
20
2973
by: tshad | last post by:
I had posted this problem earlier and just noticed that the Hyperlink is the problem. Apparently, it doesn't figure out the path correctly. It uses the path of the file it is in, even if it is...
1
4878
by: =?Utf-8?B?QWRhbQ==?= | last post by:
I'm having a problem loading an Icon into an ImageList. When I load the icon directly it works fine, like this: Icon sourceIcon = << Get an icon from somewhere... >>;...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.