How to overwrite an existing Bitmap file

using (Bitmap bitmap = new Bitmap((int)(scale * page.Width), (int)(scale * page.Height)))

{

Graphics graphics = Graphics.FromImage(bitmap);

graphics.SmoothingMode = SmoothingMode.AntiAlias;

graphics.ScaleTransform(scale, scale);

graphics.Clear(Color.White);

page.Draw(graphics);

bitmap.Save("C:\\Image.tiff", ImageFormat.Tiff);

}

As you can see in the code above, I am generating a Bitmap and saving it as image.tiff. I want to beable to re-run this piece of code over and over, but the bitmap.save does not allow you to re-write over the same file, how to I achieve this




Answer this question

How to overwrite an existing Bitmap file

  • J Kramer

    Hey PJ,

    Unfortunately that doesn't work because I still can't save over the save tiff file over and over!



  • Kevin Linder

    Why don't you create a BitmapSaver class or something. Your own helper class that can be used to save the bitmap's with some extra logic you added


    public class BitmapSaver
    {
    private BitmapSaver()
    {
    // Hide ctor.
    }

    public void Save( Bitmap bitmap )
    {
    Graphics graphics = Graphics.FromImage(bitmap);
    graphics.SmoothingMode = SmoothingMode.AntiAlias;
    graphics.ScaleTransform(scale, scale);
    graphics.Clear(Color.White);
    page.Draw(graphics);

    bitmap.Save("C:\\Image.tiff", ImageFormat.Tiff);
    }
    }




  • sticky

    Then just create a overload that excepts a string argument that represents the path to the file.


    public class BitmapSaver
    {
    private BitmapSaver()
    {
    // Hide ctor.
    }

    public void Save( Bitmap bitmap, string filename )
    {
    Graphics graphics = Graphics.FromImage(bitmap);
    graphics.SmoothingMode = SmoothingMode.AntiAlias;
    graphics.ScaleTransform(scale, scale);
    graphics.Clear(Color.White);
    page.Draw(graphics);

    bitmap.Save("filename", ImageFormat.Tiff);
    }
    }




  • How to overwrite an existing Bitmap file