Solved

Blobs

Posted on 2004-10-13
6
446 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

16 Experts available now in Live!

Get 1:1 Help Now