鸟语天空
加载本地图片
post by:追风剑情 2022-2-24 12:26
//加载本地图片
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;
}
评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容