locked
question about change image size while upload... RRS feed

  • Question

  • User1203612064 posted

    Hello, yes i know there is a lot of posts about that issue, and i read lot of them, but i can't get the results im looking for! I found lot of solutions but few in VB (im a C#), or few that create small copy of the big image, or store the image in the SQL table. The ones that look good for me didnt do the job, the image uploaded the same size. Im tryin to upload image with UploadFile control the record is writing in sql db but the image store in folder. i want that the image will upload as a small image without any small copy if its landscape - 500/310px if portrait 310/500px, Please help!!! this is the regular code im using to upload the image

    protected void addImage_Onclick(object sender, EventArgs e)
        {
    
            string File1 = FileUpload1.FileName;
            if (FileUpload1.FileName.ToString() != "")
            {
                FileUpload1.SaveAs(Server.MapPath("~\\images\\") + File1);
            }
    
            SqlConnection newconn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnBase"].ConnectionString);
            SqlCommand newCmd = new SqlCommand("INSERT INTO pagesGal ([pageId],[imgSrc]) VALUES (@p1, @p2)", newconn);
            newCmd.Parameters.Add("@p1", SqlDbType.Int).Value = Request.QueryString["pageId"];
            newCmd.Parameters.Add("@p2", SqlDbType.VarChar).Value = File1;
            newconn.Open();
            newCmd.ExecuteNonQuery();
            newconn.Close();
            newconn.Dispose();
            Response.Redirect("genGal_details.aspx?pageId=" + Request.QueryString["pageId"]);
        }



    Saturday, May 8, 2010 10:36 AM

Answers

  • User1211441112 posted

    using System.Drawing;
    using System.Drawing.Drawing2D;

            public void ResizeStream(int imageSize, Stream filePath, string outputPath)
            {
                var image = Image.FromStream(filePath);

                int thumbnailSize = imageSize;
                int newWidth, newHeight;

                if (image.Width > image.Height)
                {
                    newWidth = thumbnailSize;
                    newHeight = image.Height * thumbnailSize / image.Width;
                }
                else
                {
                    newWidth = image.Width * thumbnailSize / image.Height;
                    newHeight = thumbnailSize;
                }

                var thumbnailBitmap = new Bitmap(newWidth, newHeight);

                var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
                thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
                thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
                thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

                var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
                thumbnailGraph.DrawImage(image, imageRectangle);

                thumbnailBitmap.Save(outputPath, image.RawFormat);
                thumbnailGraph.Dispose();
                thumbnailBitmap.Dispose();
                image.Dispose();
            }

    then call resize image function as

    ResizeImage(400, File1.FileContent, path);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 10, 2010 7:44 AM

All replies

  • User-158764254 posted

    if you dont want to resize the image at the server after the upload, then you'd probably need to look at a component like one of these to do the resizing at the client:

    http://upload.thinfile.com/image/

    http://www.resize-before-upload.com/

    These are just a couple examples of components (none of which i tried myself) that turned up in this google search.

     

    Saturday, May 8, 2010 10:47 AM
  • User1203612064 posted

    thank for replay. i dont mine when the action will do the job as long as the final result will be small size image. I dont like using the sulotions youv'e send like using some plug or something, i want to use some C# code (better GDI+) to slove that. any idea will be good the bottom line need to be small size image. my problem is that not all of my users know how to use graphics software to decrease the image size so they uploading from their pc or camera 2000-4000 width px images and its look bad on the site 

    Saturday, May 8, 2010 11:27 AM
  • User-158764254 posted

    If you want to do the resize of the server, then you might want to check out the sample code posted in this thread:

    http://forums.asp.net/t/1079883.aspx 

    Saturday, May 8, 2010 11:40 AM
  • User1203612064 posted

    yes this is one of my problems..... im using C# not VB i found this tread before.

    Saturday, May 8, 2010 11:58 AM
  • User-158764254 posted

    there are online convertors that will help you convert the code

    for example:

    http://converter.telerik.com/

    http://www.developerfusion.com/tools/convert/csharp-to-vb/

    They are not perfect, but they can give you a great head-start at converting. 

    Saturday, May 8, 2010 12:06 PM
  • User1203612064 posted

    thanks for tryin, but its not working! there is an errors in the VB code so it not convertable.

    Saturday, May 8, 2010 1:43 PM
  • User1203612064 posted

    anyone? any idea?

    Saturday, May 8, 2010 11:55 PM
  • User1211441112 posted

    using System.Drawing;
    using System.Drawing.Drawing2D;

            public void ResizeStream(int imageSize, Stream filePath, string outputPath)
            {
                var image = Image.FromStream(filePath);

                int thumbnailSize = imageSize;
                int newWidth, newHeight;

                if (image.Width > image.Height)
                {
                    newWidth = thumbnailSize;
                    newHeight = image.Height * thumbnailSize / image.Width;
                }
                else
                {
                    newWidth = image.Width * thumbnailSize / image.Height;
                    newHeight = thumbnailSize;
                }

                var thumbnailBitmap = new Bitmap(newWidth, newHeight);

                var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
                thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
                thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
                thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

                var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
                thumbnailGraph.DrawImage(image, imageRectangle);

                thumbnailBitmap.Save(outputPath, image.RawFormat);
                thumbnailGraph.Dispose();
                thumbnailBitmap.Dispose();
                image.Dispose();
            }

    then call resize image function as

    ResizeImage(400, File1.FileContent, path);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 10, 2010 7:44 AM