Solved

Blobs

Posted on 2004-10-13
6
448 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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