//加载本地图片
public static Bitmap LoadBitmap(string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Bitmap bitmap = null;
Stream stream = null;
try
{
stream = new MemoryStream(bytes);
stream.Seek(0, SeekOrigin.Begin);
bitmap = new Bitmap(stream);
}
catch(ArgumentException ex)
{
//stream不是图片或为空时引发此异常
Console.WriteLine("\r\n{0};{1}", ex.Message, filePath);
}
finally
{
stream.Close();
stream = null;
bytes = null;
GC.Collect();
}
return bitmap;
}
//加载本地图片
public static Image LoadImage(string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Image image = null;
Stream stream = null;
try
{
stream = new MemoryStream(bytes);
stream.Seek(0, SeekOrigin.Begin);
image = Image.FromStream(stream);
}
catch (ArgumentException ex)
{
//stream不是图片或为空时引发此异常
Console.WriteLine("\r\n{0};{1}", ex.Message, filePath);
}
finally
{
stream.Close();
stream = null;
bytes = null;
GC.Collect();
}
return image;
}