Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Blobs

Posted on 2004-10-13
6
Medium Priority
?
461 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
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 Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses
Course of the Month12 days, 12 hours left to enroll

972 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