Solved

Blobs

Posted on 2004-10-13
6
443 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
  • 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now