Solved

Blobs

Posted on 2004-10-13
6
452 Views
Last Modified: 2008-03-06
I have a Blob stored in a database and need to be able to retrieve it from the database and stick it in a table cell so it is formated and not just printed out using response.write.

I have seen some code but cant manage to implement it succesfully.

The code must work in code behind not code within the html.

Any ideas.

This is what i had


// Code Behind //

      string sqlImage = "SELECT UserPhotoName,UserPhoto,UserPhotoType FROM tblUsers Where username=" + "'" + authUserName + "'";

SqlCommand Imagess = new SqlCommand(sqlImage, SqlConnection2);



SqlDataReader dr = Imagess.ExecuteReader();
if ( dr.Read())
{
int offset = 78;
                        
byte [] img = (byte[])dr["UserPhoto"];
MemoryStream ms = new MemoryStream();
      
ms.Write(img, offset, img.Length-offset);
                              
System.Drawing.Bitmap bmp2 = new Bitmap(ms);
Response.ContentType = "image/gif";
bmp2.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Gif);
ms.Close();

}

// Aspx Code //

<img src="Bllob.aspx">

However when i run this i get a

Invalid parameter used.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Invalid parameter used.

Source Error:


Line 118:                              
Line 119:                              
Line 120:                              System.Drawing.Bitmap bmp2 = new Bitmap(ms);
Line 121:                              Response.ContentType = "image/gif";
Line 122:                              bmp2.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Gif);
 

Please help thanks
0
Comment
Question by:WVW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12298725
Hi WVW,

read this
http://www.kbalertz.com/Feedback_317016.aspx

Regards!
B..M
0
 

Author Comment

by:WVW
ID: 12298784
Hi B..M

Unfortunately that write the image to a file and its using a windows application which is not very good considering its asp.net file and i want to write the image straight onto the page
0
 

Author Comment

by:WVW
ID: 12299006
I have tried a new approach

// aspx page //
image src  is the other aspx page i think


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
      <HEAD>
            <title>WebForm1</title>
            <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
            <meta name="CODE_LANGUAGE" Content="C#">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <IMG style="Z-INDEX: 101; LEFT: 72px; WIDTH: 384px; POSITION: absolute; TOP: 64px; HEIGHT: 320px"
                        height="320" alt="" src="blob.aspx" width="384">
            </form>
      </body>
</HTML>

// other aspx page //


<%@ Import Namespace="System" %>
<%@ Import Namespace=
   "System.Data.SqlClient" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace=
   "System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing" %>

<html>
<script runat="server">
private void Page_Load(object sender,
   System.EventArgs e)
{
        NameValueCollection col1 = ConfigurationSettings.AppSettings;
                        string strConnection1 = col1.Get("sqlConnection1.ConnectionString");
                        SqlConnection SqlConnection2 = new SqlConnection(strConnection1);
      
                        SqlConnection2.Open();
                        
   String cmdText = "SELECT UserPhotoName,UserPhoto,UserPhotoType FROM tblUsers Where username='Administrator'";
   SqlCommand cmd = new SqlCommand(cmdText, SqlConnection2);

   MemoryStream ms = new
      MemoryStream();

   // 78 is the size of the OLE header
   // for Northwind images.
   // There's no header in PUBS as PUBS
   // just contains the raw image bits.
   int offset = 0;

   SqlConnection2.Open();
   byte [] img = (byte[])
      cmd.ExecuteScalar();
   ms.Write(img, offset,
      img.Length-offset);
   SqlConnection2.Close();

   Bitmap bmp = null;
   bmp = new Bitmap(ms);
   Response.ContentType = "image/gif";
   bmp.Save(Response.OutputStream,
      ImageFormat.Gif);
   ms.Close();
}
</script>
</html>

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:WVW
ID: 12299266
I have solved the problem

string sqlImage = "SELECT UserPhotoName,UserPhoto,UserPhotoType FROM tblUsers Where username=" + "'" + authUserName + "'";
      SqlCommand Imagess = new SqlCommand(sqlImage, SqlConnection2);

      SqlDataReader dr = Imagess.ExecuteReader();
      if ( dr.Read())
      {
            //Response.ContentType = dr["img_contenttype"].ToString();
            
            


            Response.BinaryWrite( (byte[]) dr["UserPhoto"] );
      }


this works fine

Thanks
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12305326
I'm sorry that i didn't left anything about your posts but i was out of computer in the last 12 hours :(
glad that you have solved the problem

Regards!
B..M
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 12421780
Question answered by asker or dialog valuable.
Closed, 400 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question