Click here to Skip to main content
15,793,609 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
i want to save an image in server but i got this error message
C#
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["bongahConnectionString"].ConnectionString);
        con.Open();
        string query1 = "SELECT ID FROM aparteman WHERE ID=(SELECT MAX(ID) FROM aparteman)";
        SqlCommand cmd = new SqlCommand(query1, con);
        SqlDataReader file = null;
        file = cmd.ExecuteReader();
        while (file.Read())
        {
             Filename1 = file["ID"].ToString();
        }
        con.Close();
        if (fileupload1.HasFile)
        {

            string fileName = Server.HtmlEncode(fileupload1.FileName);
            string extension = System.IO.Path.GetExtension(fileName);
            System.Drawing.Image image_file = System.Drawing.Image.FromStream(fileupload1.PostedFile.InputStream);
            
            int image_height = image_file.Height;
            int image_width = image_file.Width;
            int max_height = 300;
            int max_width = 500;

            image_height = (image_height * max_width) / image_width;
            image_width = max_width;

            if (image_height > max_height)
            {
                image_width = (image_width * max_height) / image_height;
                image_height = max_height;
            }
            Int32 tem = Convert.ToInt32(Filename1) + 1;
            Filename1 = Convert.ToString(tem);
            Bitmap bitmap_file = new Bitmap(image_file, image_width, image_height);
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            filename = Path.GetFileName(fileupload1.PostedFile.FileName);
            bitmap_file.Save(Server.MapPath("~/uploadimage/Aparteman/"+Filename1+".jpg"));
Posted

Without knowing which part of this generate the error - and you error message will be specific to the line, and possibly column - we can't be specific.

But start by checking the uploaded file and make sure that it is an Image file: if it isn't then it isn't going to work.

But...why are you creating an Image from the upload if all you want to do is save the file? Why not just save the data, unless you are going to display it at the same time? (and even then, I'd probably use the same mechanism I was going to use to display it on a web page under =other circumstances - so why create an Image and a Bitmap anyway?
 
Share this answer
 
Comments
m-e-h-d-h-i 6-Jun-14 3:07am    
this line
newBitmap.Save(Server.MapPath("~/uploadimage/Aparteman/"+Filename1+".jpg"));
And i am sure that uploaded file is an image
I only return from a stream because after using the resize code similar to this the destination file has an unknown mime type (img.RawFormat.Guid) and Id like the Mime type to be correct on all image objects as it makes it hard write generic handling code otherwise.
The Trick for making PNGs: Without the extra intermediate MemoryStream to save to, you won't be able to make PNGs. You can't image.Save() a PNG directly to Response.OutputStream.
bitmap_file.Save(Server.MapPath("~/uploadimage/Aparteman/")+Filename1+".png", ImageFormat.Png); 

See the following links
resize-transparent-images-using-c-sharp[^]

Scott[^]
 
Share this answer
 

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