Saving a modified image to the original file using GDI+

我与影子孤独终老i 提交于 2019-12-05 12:54:41

Well if you're looking for other ways to do what you're asking, I reckon it should work to create a MemoryStream, and read out the FileStream to it, and load the Image from that stream...

var stream = new FileStream("original-image", FileMode.Open);
var bufr = new byte[stream.Length];
stream.Read(bufr, 0, (int)stream.Length);
stream.Dispose();

var memstream = new MemoryStream(bufr);
var image = Image.FromStream(memstream);

Or something prettier to that extent.

Whether or not that's the way you should go about solving that problem, I don't know. :) I've had a similar problem and wound up fixing it like this.

I have since found an alternative method to clone the image without locking the file. Bob Powell has it all plus more GDI resources.

      //open the file
      Image i = Image.FromFile(path);

      //create temporary
      Image t=new Bitmap(i.Width,i.Height);

      //get graphics
      Graphics g=Graphics.FromImage(t);

      //copy original
      g.DrawImage(i,0,0);

      //close original
      i.Dispose();

      //Can now save
      t.Save(path)

I had a similar problem. But I knew, that I will save the image as a bitmap-file. So I did this:

    public void SaveHeightmap(string path)
    {
        if (File.Exists(path))
        {
            Bitmap bitmap = new Bitmap(image); //create bitmap from image
            image.Dispose(); //delete image, so the file

            bitmap.Save(path); //save bitmap

            image = (Image) bitmap; //recreate image from bitmap
        }
        else
            //...
    }

Sure, thats not the best way, but its working :-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!