Click here to Skip to main content
15,033,303 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
My code is upload the image and save the image in binary format


jpeg format image is save and also displayed

problem is that Gif format image is not stored and displayed
this my image upload Code

string strImageName = TextBox1.Text.ToString();
       if (!object.Equals(Session["Id"], null))
       {
           if (Object.Equals(Session["Id"], Request.QueryString["Id"].ToString()))
           {
               if (FileUpload2.HasFile)
               {
                   string ImageName = FileUpload2.PostedFile.FileName;
                   string sImageFileExtension = ImageName.Substring(ImageName.LastIndexOf(".")).ToLower();
                   if (sImageFileExtension == ".Gif" || sImageFileExtension == ".png" || sImageFileExtension == ".jpg" || sImageFileExtension == ".jpeg" || sImageFileExtension == ".bmp")
                   {
                       // int length = FileUpload1.PostedFile.ContentLength;
                       //    string strExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
                       //  if ((strExtension.ToUpper() == ".JPG") | (strExtension.ToUpper() == ".GIF"))

                       // Resize Image Before Uploading to DataBase
                       System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(FileUpload2.PostedFile.InputStream);

                       int imageHeight = imageToBeResized.Height;
                       int imageWidth = imageToBeResized.Width;
                       int maxHeight = 450;
                       int maxWidth = 320;
                       if (imageWidth > maxWidth)
                       {
                           imageHeight = (imageHeight * maxWidth) / imageWidth;
                           imageWidth = maxWidth;
                       }
                       if (imageHeight > maxHeight)
                       {
                           imageWidth = (imageWidth * maxHeight) / imageHeight;
                           imageHeight = maxHeight;
                       }
                       Bitmap bitmap = new Bitmap(imageToBeResized, imageWidth, imageHeight);//existing image Scaled to the specified size
                       System.IO.MemoryStream stream = new MemoryStream();
                       bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                       bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);here is problem may  be image Gif format is not supported
                       stream.Position = 0;

                       //create a byte array to store the binary image data
                       byte[] image = new byte[stream.Length + 1];
                       //set the binary data
                       stream.Read(image, 0, image.Length);
                       SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\omar\Documents\Visual Studio 2005\WebSites\Project2\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
                       string que = "insert into Pictures(Id,ImageName,Image)values('" + Session["Id"] + "',@imagename,@image )";
                       SqlCommand com = new SqlCommand(que, con);
                       com.Parameters.Add("@imagename", SqlDbType.VarChar, 500);
                       com.Parameters.Add("@image", SqlDbType.Image, image.Length);
                       // com.Parameters.Add("@image1", SqlDbType.Image, image.Length);
                       com.Parameters["@imagename"].Value = strImageName.ToString();
                       com.Parameters["@image"].Value = image;
                       // com.Parameters["@image1"].Value = image;
                       con.Open();
                       com.ExecuteNonQuery();
                       con.Close();
                       // Response.Redirect("Default3.aspx");
                       //Response.Redirect("UserDetails.aspx");
                   }
               }
           }
       }
   }


Handle code is

<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\omar\Documents\Visual Studio 2005\WebSites\Project2\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
       // con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        // Create SQL Command 
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Select Image from Pictures where ImgId =@ImgId";
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Connection = con;
        SqlParameter ImageID = new SqlParameter("@ImgId", System.Data.SqlDbType.Int);
        ImageID.Value = context.Request.QueryString["ImgId"];
        cmd.Parameters.Add(ImageID);
        con.Open();
       // int intImg = Convert.ToInt32(context.Request.QueryString["img"]);
        SqlDataReader dReader = cmd.ExecuteReader();
        dReader.Read();
        context.Response.BinaryWrite((byte[])dReader["Image"]);
        dReader.Close();
  
        con.Close();
   
    }
Posted
Updated 20-May-11 3:03am

1 solution

sImageFileExtension == ".Gif"

should be
sImageFileExtension == ".gif"

since you are calling ToLower on it the line above. This is the sort of thing you ought to be able to find yourself, since you had already worked out it was GIFs that were failing. It would have been far quicker than typing up a post here and then waiting for someone to reply.
   
v2
Comments
Manfred Rudolf Bihy 20-May-11 10:26am
   
Good find! 5+
I already stopped reading after the title: "Code is working plz ...". Why would I want to read code that is working. If it works there is not need to check! :)
BobJanova 20-May-11 12:48pm
   
Yeah I almost skipped past it as well. I gave it the fifteen seconds to scan the thing that the OP mentioned, though. Questions like this are kind of annoying.
Sergey Alexandrovich Kryukov 20-May-11 16:41pm
   
Right, my 5, too.
Overall, this activity is not very effective. Imagine, one fix in one line at a time, new web page in CodeProject for each. Yes, with such development skills it's not much faster. That's why I often recommend to learn development first of very simple problems, but in depth.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900