Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hi,
i have written a code for saving original image and saving resized image in database using asp.net c#.
 string imgName = FileUpload1.FileName.ToString();
      //sets the image path
        string imgPath = "~/upload/" + imgName;
        string imgPath2 = "~/small upload/" + imgName;
        //get the size in bytes that
        filepath = Server.MapPath(imgPath);
        filepath2 = Server.MapPath(imgPath2);
        FileUpload1.SaveAs(Server.MapPath(imgPath));
        ResizeImage(30,filepath,filepathsmall);



        FileStream fs = new FileStream(filepath2, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        Byte[] bytes = br.ReadBytes((Int32)fs.Length);
        br.Close();
        fs.Close();
        string strQuery = "INSERT INTO products(name, description, image,category,price,imagepath) VALUES(@NM, @EM, @PN,@CT,@PC,@IP)";
        SqlCommand cmd = new SqlCommand(strQuery);
        cmd.Parameters.AddWithValue("@NM", name);
        cmd.Parameters.AddWithValue("@EM", desc);
        cmd.Parameters.AddWithValue("@PN", bytes);
        cmd.Parameters.AddWithValue("@CT", category);
        cmd.Parameters.AddWithValue("@PC", price);
        cmd.Parameters.AddWithValue("@IP", imgPath2);
        InsertData(cmd);

Functions
public static void ResizeImage(int size, string filePath, string saveFilePath)
    {
        //variables for image dimension/scale
        double newHeight = 0;
        double newWidth = 0;
        double scale = 0;

        //create new image object
        Bitmap curImage = new Bitmap(filePath);

        //Determine image scaling
        if (curImage.Height > curImage.Width)
        {
            scale = Convert.ToSingle(size) / curImage.Height;
        }
        else
        {
            scale = Convert.ToSingle(size) / curImage.Width;
        }
        if (scale < 0 || scale > 1) { scale = 1; }

        //New image dimension
        newHeight = Math.Floor(Convert.ToSingle(curImage.Height) * scale);
        newWidth = Math.Floor(Convert.ToSingle(curImage.Width) * scale);

        //Create new object image
        Bitmap newImage = new Bitmap(curImage, Convert.ToInt32(newWidth), Convert.ToInt32(newHeight));
        Graphics imgDest = Graphics.FromImage(newImage);
        imgDest.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        imgDest.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        imgDest.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
        imgDest.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
        EncoderParameters param = new EncoderParameters(1);
        param.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);

        //Draw the object image
        imgDest.DrawImage(curImage, 0, 0, newImage.Width, newImage.Height);

        //Save image file
        newImage.Save(saveFilePath, info[1], param);

        //Dispose the image objects
        curImage.Dispose();
        newImage.Dispose();
        imgDest.Dispose();
    }


can you tell me in which format should we give size factor.when i compile i am getting error
A generic error occurred in GDI+.
can you help me solve this eeror or suggest me another code
Posted
Comments
Marc Gabrie 29-Jan-14 6:25am    
what image format (PNG, JPG, TIF, etc) are your images and what is the size in both W&H (pixels) and in bytes?

Reason

Quote:
When either a Bitmap object or an Image object is constructed from a file, the file remains locked for the lifetime of the object. As a result, you cannot change an image and save it back to the same file where it originated
from Quote Reference[^]

Solution,refer below link :
Refer[^]
 
Share this answer
 
v2

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