Saturday, February 5, 2011

Save Image to Disk using C#.Net

After lot of search I found the code that saves Image class to harddisk.

public void SaveImage(Image imgToSave, string FileName)
        {
            //Create empty bitmap image of original size

            Bitmap tempBmp = new Bitmap(imgToSave.Width, imgToSave.Height);

            Graphics g = Graphics.FromImage(tempBmp);
            //draw the original image on tempBmp

            g.DrawImage(imgToSave, 0, 0, imgToSave.Width, imgToSave.Height);


            //dispose originalImage and Graphics so the file is now free

            g.Dispose();

            imgToSave.Dispose();

            //Save the image file to original location

            tempBmp.Save(FileName, System.Drawing.Imaging.ImageFormat.Jpeg);

            
        }

No comments: