Click here to Skip to main content
15,888,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My project run correctly but when publish it show generic error on this code, how to solve it.

C#
protected void txtBrightness_TextChanged(object sender, EventArgs e)
       {
           string filename = Server.MapPath("Sample.jpg");
           string toFilename = Server.MapPath("brightImage.jpg");
           Bitmap bmp = new Bitmap(filename);
           AdjustBrightness(bmp, Convert.ToInt16(txtBrightness.Text), toFilename);
           bmp.Dispose();
           Image1.ImageUrl = "~/brightImage.jpg";
       }



C#
public void AdjustBrightness(Bitmap Image, int Value, string toFileName)
        {
            System.Drawing.Bitmap TempBitmap = Image;
            float FinalValue = (float)Value / 255.0f;
            System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
            System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
            float[][] FloatColorMatrix ={
                    new float[] {1, 0, 0, 0, 0},
                    new float[] {0, 1, 0, 0, 0},
                    new float[] {0, 0, 1, 0, 0},
                    new float[] {0, 0, 0, 1, 0},
                    new float[] {FinalValue, FinalValue, FinalValue, 1, 1}
                };

            System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(FloatColorMatrix);
            System.Drawing.Imaging.ImageAttributes Attributes = new System.Drawing.Imaging.ImageAttributes();
            Attributes.SetColorMatrix(NewColorMatrix);
            NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, System.Drawing.GraphicsUnit.Pixel, Attributes);
            Attributes.Dispose();
            NewGraphics.Dispose();
            NewBitmap.Save(toFileName);
            NewBitmap.Dispose();
        }


Edit: Copied additional info from comments below
Exception is thrown at line: NewBitmap.Save(toFileName);

on debugging mode is run perfectly not showing error.
Posted
Updated 14-Apr-12 7:36am
v4
Comments
OriginalGriff 14-Apr-12 3:01am    
Where does it throw the exception? Which line?
balagee99 13-Jun-12 8:05am    
me too facing same problem while saving image file to same path using
Image.FromFile("C:\\sample.png")
OriginalGriff 13-Jun-12 8:11am    
You should raise this as a separate question - it's considered rude to just "but in" with a different problem.
But yours is very simple, so I'll answer this time: From Vista onwards, the root directory of the boot drive requires Admin privileges to write to. Since you App doesn't have them, you get an error. Save the image to a different drive, or to a different folder and you'll be fine.
Jitendra Parida - Jeetu 14-Apr-12 3:05am    
NewBitmap.Save(toFileName);
Wonde Tadesse 14-Apr-12 13:42pm    
What is the exception, stacktrace ... ?

1 solution

Try debugging and stepping through your code line by line - once you get to the line that throws the error, try to check the values at that point. This should give you an idea of what the error could be.
 
Share this answer
 
Comments
Jitendra Parida - Jeetu 14-Apr-12 3:04am    
on debugging mode is run perfectly not showing error.

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