您的位置:首页 > 编程语言 > C#

C#获取图像并删除图像

2009-06-17 11:06 204 查看
经常使用PictureBoxPhoto.image = Image.FromFile(pbPath);

此时要删除此图像,往往会报此文件正在被占用的错误!

采取文件流读取方式后解决此问题

/// <summary>
/// 根据图像名称获得图像(从文件流读取,所以原图像可以删除)
/// </summary>
/// <param name="PhotoPath"></param>
/// <returns></returns>
public static Bitmap GetImageFromStream(string PhotoPath)
{
Bitmap bmp = null;
try
{
FileStream fs = new FileStream(PhotoPath, FileMode.OpenOrCreate, FileAccess.Read);
byte[] theData = new byte[fs.Length];
fs.Read(theData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();

System.IO.MemoryStream stream = new System.IO.MemoryStream(theData, true);
stream.Write(theData, 0, theData.Length);
bmp = new Bitmap(stream);
}
catch
{
bmp = null;
}
return bmp;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: