我从本地文件中创建了一个bitmap后,Bitmap a = new Bitmap(strImagePath);  这时,此文件被程序给占用,无法修改删除等,现在想解除该图片的锁定状态,但是又不释放内存中的资源,该如何做呢?    用a.Dispose(),不但释放了文件,也把内存中的资源给释放了,
    用clone:
    Bitmap b = a.clone();
    a.Dispose();
    发现文件仍然处于锁定状态
    难道要用SetPixel和GetPixel复制图片,然后在Dispose,才能解除图片的锁定吗?
    
    
            

解决方案 »

  1.   

    System.IO.FileStream file=new System.IO.FileStream(YourFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
                Bitmap image = (Bitmap)Bitmap.FromStream(file);
                file.Dispose();
      

  2.   

                System.IO.FileStream fs = new System.IO.FileStream("e:\\2.jpg", System.IO.FileMode.Open);
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
                fs.Dispose();
                System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
                Bitmap bmp1 =new Bitmap(ms);
      

  3.   

                System.IO.FileStream fs = new System.IO.FileStream("e:\\2.jpg", System.IO.FileMode.Open);
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
                fs.Dispose();
                System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
                Bitmap bmp1 =new Bitmap(ms);