.NETGURU
How to Save a generated image to SQL Server
Messages   Related Types
This message was discovered on ASPFriends.com 'ngfx-drawing' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Tony Stephenson
Could someone please shed some light on how to save a code-generated
bitmap to a SQL Server DB?

My aim is to generate a number of pie charts, save them to a db, then
display them in a datalist.

I am able to generate and display a pie chart OK (from data I provide),
but what I can't do is find a way of saving it.

This is the code I have to generate the image:

objBitmap contains the generated pie chart

MemoryStream ms = new MemoryStream();
objBitmap.Save(ms,
System.Drawing.Imaging.ImageFormat.Gif);
imgLen = Convert.ToInt32(ms.Length);
string imgName = title;
imgContentType = "image/gif";

BinaryReader bw= new BinaryReader(ms);
b = new Byte[imgLen];
bw.Read(b, 0, imgLen);
imgName_value = imgName;
string lblNewRec = (new
MysinessObject((StandardBusinessObject)Session["StandardBusinessObject"]
)).AddImage(Convert.ToInt32(Session["RecID imgName_value, b,
imgContentType);

objGraphics.Dispose();
objBitmap.Dispose();

This is the code behind the page used to display the image:

namespace MyWebPages
{
using System;
using System.Collections;
using System.IO;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.Serialization.Formatters.Binary;
using AppraiseBusinessObjects;

public class ImageGenerator3 : System.Web.UI.Page
{

public System.Data.DataRowView dvImage;

public void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}

}

public void BindData()
{
dvImage = new
MyBusinessObject((StandardBusinessObject)Session["StandardBusinessObject
"]).GetSingleImage(Convert.ToInt32(Request.QueryString["id"]));
Response.ContentType dvImage["Img_ContentType"].ToString();
Response.BinaryWrite((byte[])(dvImage["Img_Data"]));
Response.Flush ();
Response.End ();
}

}

}

This code appears to work: the record is created in the database, and I
can retrieve it, however the image control displays an "X". I must be
doing something wrong somewhere, and I have tried a zillion ways to do
this.

I hope some out there can see the obvious mistake(s!), or have some code
they can share, for me to adapt

TIA

Tony

Email: Click here to reveal e-mail address
Reply to this message...
 
    
Kloberg Mac (LAM) (VIP)
What you're describing appears quite complex, but I think I got the idea.
The red "X" you're getting in the browser hints that the client is actually
getting some kind of data, but it is either invalid (as in GIF/JPG encoding)
or it doesn't know what to do with it.

Make sure the ContentType is valid (as in "image/gif") and that the browser
really gets it (code: it's coming from a datarow, so you might want to
verify that this really comes out as such).
Other than that, the problem might be in your "MysinessObject" class. What
always helps is to go back to the roots, save the content of the saved field
to a file, rename it to .gif and open it in a browser/editor and see what
happens...

hth
--
Sincerely,
Mac Kloberg
LIEBHERR AMERICA INC.
http://www.liebherr-us.com <http://www.liebherr-us.com/>
-----Original Message-----
From: Tony Stephenson [mailto:Click here to reveal e-mail address]
Sent: Saturday, July 20, 2002 7:22 AM
To: ngfx-drawing
Subject: [ngfx-drawing] How to Save a generated image to SQL Server

Could someone please shed some light on how to save a code-generated bitmap
to a SQL Server DB?

My aim is to generate a number of pie charts, save them to a db, then
display them in a datalist.

I am able to generate and display a pie chart OK (from data I provide), but
what I can't do is find a way of saving it.

This is the code I have to generate the image:

objBitmap contains the generated pie chart

MemoryStream ms = new MemoryStream();
objBitmap.Save(ms,
System.Drawing.Imaging.ImageFormat.Gif);
imgLen = Convert.ToInt32(ms.Length);
string imgName = title;
imgContentType = "image/gif";

BinaryReader bw= new BinaryReader(ms);
b = new Byte[imgLen];
bw.Read(b, 0, imgLen);
imgName_value = imgName;
string lblNewRec = (new
MysinessObject((StandardBusinessObject)Session["StandardBusinessObject"])).A
ddImage(Convert.ToInt32(Session["RecID imgName_value, b, imgContentType);

objGraphics.Dispose();
objBitmap.Dispose();

This is the code behind the page used to display the image:

namespace MyWebPages
{
using System;
using System.Collections;
using System.IO;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.Serialization.Formatters.Binary;
using AppraiseBusinessObjects;

public class ImageGenerator3 : System.Web.UI.Page
{

public System.Data.DataRowView dvImage;

public void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}

}

public void BindData()
{
dvImage = new
MyBusinessObject((StandardBusinessObject)Session["StandardBusinessObject"]).
GetSingleImage(Convert.ToInt32(Request.QueryString["id"]));
Response.ContentType dvImage["Img_ContentType"].ToString();
Response.BinaryWrite((byte[])(dvImage["Img_Data"]));
Response.Flush ();
Response.End ();
}

}

}

This code appears to work: the record is created in the database, and I can
retrieve it, however the image control displays an "X". I must be doing
something wrong somewhere, and I have tried a zillion ways to do this.

I hope some out there can see the obvious mistake(s!), or have some code
they can share, for me to adapt

TIA

Tony

Email: Click here to reveal e-mail address

| [ngfx-drawing] member Click here to reveal e-mail address = YOUR ID |
http://www.aspfriends.com/aspfriends/ngfx-drawing.asp = JOIN/QUIT
Reply to this message...
 
    
Kloberg Mac (LAM) (VIP)
Something else: Try to do a "Response.ClearContent()" before writing to the
response stream...
--
Sincerely,
Mac Kloberg
LIEBHERR AMERICA INC.
http://www.liebherr-us.com <http://www.liebherr-us.com/>
-----Original Message-----
From: Tony Stephenson [mailto:Click here to reveal e-mail address]
Sent: Saturday, July 20, 2002 7:22 AM
To: ngfx-drawing
Subject: [ngfx-drawing] How to Save a generated image to SQL Server

Could someone please shed some light on how to save a code-generated bitmap
to a SQL Server DB?

My aim is to generate a number of pie charts, save them to a db, then
display them in a datalist.

I am able to generate and display a pie chart OK (from data I provide), but
what I can't do is find a way of saving it.

This is the code I have to generate the image:

objBitmap contains the generated pie chart

MemoryStream ms = new MemoryStream();
objBitmap.Save(ms,
System.Drawing.Imaging.ImageFormat.Gif);
imgLen = Convert.ToInt32(ms.Length);
string imgName = title;
imgContentType = "image/gif";

BinaryReader bw= new BinaryReader(ms);
b = new Byte[imgLen];
bw.Read(b, 0, imgLen);
imgName_value = imgName;
string lblNewRec = (new
MysinessObject((StandardBusinessObject)Session["StandardBusinessObject"])).A
ddImage(Convert.ToInt32(Session["RecID imgName_value, b, imgContentType);

objGraphics.Dispose();
objBitmap.Dispose();

This is the code behind the page used to display the image:

namespace MyWebPages
{
using System;
using System.Collections;
using System.IO;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.Serialization.Formatters.Binary;
using AppraiseBusinessObjects;

public class ImageGenerator3 : System.Web.UI.Page
{

public System.Data.DataRowView dvImage;

public void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}

}

public void BindData()
{
dvImage = new
MyBusinessObject((StandardBusinessObject)Session["StandardBusinessObject"]).
GetSingleImage(Convert.ToInt32(Request.QueryString["id"]));
Response.ContentType dvImage["Img_ContentType"].ToString();
Response.BinaryWrite((byte[])(dvImage["Img_Data"]));
Response.Flush ();
Response.End ();
}

}

}

This code appears to work: the record is created in the database, and I can
retrieve it, however the image control displays an "X". I must be doing
something wrong somewhere, and I have tried a zillion ways to do this.

I hope some out there can see the obvious mistake(s!), or have some code
they can share, for me to adapt

TIA

Tony

Email: Click here to reveal e-mail address

| [ngfx-drawing] member Click here to reveal e-mail address = YOUR ID |
http://www.aspfriends.com/aspfriends/ngfx-drawing.asp = JOIN/QUIT
Reply to this message...
 
    
Das
I am not sure whether my articles on "images and Sql Server" will help you
out or not. But give it a try.

Inserting Images to SqlServer in ASP .NET
http://aspalliance.com/das/insertimage.aspx" target="_blank">http://aspalliance.com/das/insertimage.aspx

Retrieving Images from SqlServer in ASP .NET
http://aspalliance.com/das/readimage.aspx" target="_blank">http://aspalliance.com/das/readimage.aspx

Retrieving Images from SqlServer and displaying in a DataGrid - ASP .NET
http://aspalliance.com/das/datagridimages.aspx" target="_blank">http://aspalliance.com/das/datagridimages.aspx

:-)
Jesudas Chinnathampi (Das)

Co-Author - Beginning ASP.NET Databases using VB.NET
http://www.amazon.com/exec/obidos/ASIN/1861006195

Read my articles on ASP .NET at
http://aspalliance.com/das

----- Original Message -----
From: "Tony Stephenson" <Click here to reveal e-mail address>
To: "ngfx-drawing" <Click here to reveal e-mail address>
Sent: Saturday, July 20, 2002 7:22 AM
Subject: [ngfx-drawing] How to Save a generated image to SQL Server

[Original message clipped]

Reply to this message...
 
    
Tony Stephenson
Thanks Mac

Well, I tried saving the generated gifs to files rather than the db, and
sure enough, all (6 in this case) pie charts were created, and could be
opened with IE
Still no luck with saving then extracting the images from the db.
The code I am using to extract (datarowview etc) and display the images
is exactly the same as that I have used on another page to display
images that I have uploaded and saved.

So, I know that the images are being created OK. I also know that db
records are being created in my sql db. Its only when I try to extract
and display the images that the problem seems to occur,
I suspect it is at the point where I take the imgBitmap and read it into
a byte array that the problem is happening, but I don't know.
I have spent the weekend searching the net in vain for just one example
of where someone has created an image, then saved it to a db.

Hasn't anyone done this?

By the way I tried a "Response.ClearContent()" to no avail.

Tony Stephenson
Director
Performance Management Systems Pty Ltd
Email: Click here to reveal e-mail address
Phone: 61 3 9511 5283
Fax: 61 3 9511 5284
Web: http://www.persyst.com.au <http://www.persyst.com.au/" target="_blank">http://www.persyst.com.au/>

-----Original Message-----
From: Kloberg Mac (LAM) [mailto:Click here to reveal e-mail address]
Sent: Sunday, 21 July 2002 12:21 PM
To: ngfx-drawing
Subject: [ngfx-drawing] RE: How to Save a generated image to SQL Server

What you're describing appears quite complex, but I think I got the
idea.
The red "X" you're getting in the browser hints that the client is
actually getting some kind of data, but it is either invalid (as in
GIF/JPG encoding) or it doesn't know what to do with it.

Make sure the ContentType is valid (as in "image/gif") and that the
browser really gets it (code: it's coming from a datarow, so you might
want to verify that this really comes out as such).
Other than that, the problem might be in your "MysinessObject" class.
What always helps is to go back to the roots, save the content of the
saved field to a file, rename it to .gif and open it in a browser/editor
and see what happens...

hth
--
Sincerely,
Mac Kloberg
LIEBHERR AMERICA INC.
http://www.liebherr-us.com <http://www.liebherr-us.com/>
-----Original Message-----
From: Tony Stephenson [mailto:Click here to reveal e-mail address]
Sent: Saturday, July 20, 2002 7:22 AM
To: ngfx-drawing
Subject: [ngfx-drawing] How to Save a generated image to SQL Server
Could someone please shed some light on how to save a code-generated
bitmap to a SQL Server DB?

My aim is to generate a number of pie charts, save them to a db, then
display them in a datalist.

I am able to generate and display a pie chart OK (from data I provide),
but what I can't do is find a way of saving it.

This is the code I have to generate the image:

objBitmap contains the generated pie chart

MemoryStream ms = new MemoryStream();
objBitmap.Save(ms,
System.Drawing.Imaging.ImageFormat.Gif);
imgLen = Convert.ToInt32(ms.Length);
string imgName = title;
imgContentType = "image/gif";

BinaryReader bw= new BinaryReader(ms);
b = new Byte[imgLen];
bw.Read(b, 0, imgLen);
imgName_value = imgName;
string lblNewRec = (new
MysinessObject((StandardBusinessObject)Session["StandardBusinessObject"]
)).AddImage(Convert.ToInt32(Session["RecID imgName_value, b,
imgContentType);

objGraphics.Dispose();
objBitmap.Dispose();

This is the code behind the page used to display the image:

namespace MyWebPages
{
using System;
using System.Collections;
using System.IO;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.Serialization.Formatters.Binary;
using AppraiseBusinessObjects;

public class ImageGenerator3 : System.Web.UI.Page
{

public System.Data.DataRowView dvImage;

public void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}

}

public void BindData()
{
dvImage = new
MyBusinessObject((StandardBusinessObject)Session["StandardBusinessObject
"]).GetSingleImage(Convert.ToInt32(Request.QueryString["id"]));
Response.ContentType dvImage["Img_ContentType"].ToString();
Response.BinaryWrite((byte[])(dvImage["Img_Data"]));
Response.Flush ();
Response.End ();
}

}

}

This code appears to work: the record is created in the database, and I
can retrieve it, however the image control displays an "X". I must be
doing something wrong somewhere, and I have tried a zillion ways to do
this.

I hope some out there can see the obvious mistake(s!), or have some code
they can share, for me to adapt

TIA

Tony

Email: Click here to reveal e-mail address

| [ngfx-drawing] member Click here to reveal e-mail address = YOUR ID |
http://www.aspfriends.com/aspfriends/ngfx-drawing.asp = JOIN/QUIT
| [ngfx-drawing] member Click here to reveal e-mail address = YOUR ID |
http://www.aspfriends.com/aspfriends/ngfx-drawing.asp = JOIN/QUIT
Reply to this message...
 
 
System.Byte
System.Convert
System.Data.DataRowView
System.Drawing.Imaging.ImageFormat
System.EventArgs
System.IO.BinaryReader
System.IO.MemoryStream
System.Web.UI.Page
System.Web.UI.WebControls.DataGrid
System.Windows.Forms.DataGrid




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification