CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2011
    Posts
    4

    Creating Thumbnail

    I had managed to let users upload the image and save into the database and the folder. I need to generate thumbnail from the image and save into the database and the folder as well. But there is some error with generating thumbnail. Can someone help with me with it? Thank you

    Code:
    protected void Button1_Click(object sender, EventArgs e)
            {
                //Get Filename from fileupload control
                string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
                //Save images into Images folder
                FileUpload1.SaveAs(Request.MapPath("/Images/uploaded/" + filename));
                //Getting dbconnection from web.config connectionstring
                SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
                using (SqlDataAdapter adapter = new SqlDataAdapter("select ImageName from Images", connection))
                //Open the database connection
                {
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    DataList1.DataSource = dt;
                    DataList1.DataBind();
                }
                connection.Open();
                //Query to insert images path and name into database
                SqlCommand cmd = new SqlCommand("Insert into Images(ImageName) values(@ImageName)", connection);
                //Passing parameters to query
                cmd.Parameters.AddWithValue("@ImageName", filename);
                cmd.ExecuteNonQuery();
                //Close dbconnection
                connection.Close();
                string strImage = FileUpload1.FileName.ToString();
                if(FileUpload1.HasFile)
                {
                strImage = FileUpload1.FileName.ToString();
                //check whether the file exists or not
                if (!File.Exists(Request.MapPath("/Images/uploaded/") + strImage)) //testing
                {
                
               //create a new file name with grid
                    strImage = Guid.NewGuid().ToString().Substring(0,8) + strImage.Substring(strImage.IndexOf('.'));
          
                }
                //save the file
                FileUpload1.SaveAs(Request.MapPath("/Images/uploaded/") + strImage); //testing
                // create an image object, using the filename we just retrieved
                System.Drawing.Image image = System.Drawing.Image.FromFile(Request.MapPath("/Images/uploaded/"));
                // create the actual thumbnail image
                System.Drawing.Image thumbnailImage = image.GetThumbnailImage(64, 64, new                                 System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
                // make a memory stream to work with the image bytes
                MemoryStream imageStream = new MemoryStream();
                // put the image into the memory stream
                thumbnailImage.Save(imageStream, System.Drawing.Imaging.Imageformat.Jpeg);
          
                } 
             }
    Error message - The name 'ThumbnailCallback' does not exist in the current context.
    The type or namespace name 'Imageformat' does not exist in the namespace 'System.Drawing.Imaging' (are you missing an assembly reference?).
    Do I need to create a folder to store the thumbnail?
    Last edited by GremlinSA; November 6th, 2011 at 03:09 PM. Reason: Added code tags..

  2. #2
    Join Date
    Feb 2002
    Location
    Mumbai, India
    Posts
    242

    Re: Creating Thumbnail

    Yes you need to have have Images\Uploaded folders under your website.
    Also, you need to make the following changes to the code

    Original
    Code:
    System.Drawing.Image image = System.Drawing.Image.FromFile(Request.MapPath("/Images/uploaded/"));
    Change it to
    Code:
    System.Drawing.Image image = System.Drawing.Image.FromFile(Request.MapPath("/Images/uploaded/") + strImage);
    Original Code
    Code:
    thumbnailImage.Save(imageStream, System.Drawing.Imaging.Imageformat.Jpeg);
    Change it to
    Code:
    thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);

    Add the following function
    Code:
    public bool ThumbnailCallback()
            {
                return true;
            }
    Please let me know if the code does not work
    Hope this helps. If it does, then rate it.
    ----
    Rohit

  3. #3
    Join Date
    Oct 2011
    Posts
    4

    Re: Creating Thumbnail

    Original Code
    Code:
    thumbnailImage.Save(imageStream, System.Drawing.Imaging.Imageformat.Jpeg);
    Change it to
    Code:
    thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);

    May I know what is the original code source? (because both of them are the same)

    AND

    public bool ThumbnailCallback()
    {
    return true;
    }

    Do I replace with this or should I put at the back?-

    protected void btnGenerateThumbNail_Click(object sender, EventArgs e)
    {
    }

  4. #4
    Join Date
    Feb 2002
    Location
    Mumbai, India
    Posts
    242

    Re: Creating Thumbnail

    Difference between the original (Imaging.Imageformat) and new code (Imaging.ImageFormat) is 'F' in capital

    Place ThumbnailCallback function after the button click is completed i.e. after

    Code:
    protected void btnGenerateThumbNail_Click(object sender, EventArgs e)
    {
    //You rest of the code
    }
    Hope this helps. If it does, then rate it.
    ----
    Rohit

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured