pictureman.cs

来自「考试管理系统CS架构 不需更多说明」· CS 代码 · 共 85 行

CS
85
字号
using System;
using System.Drawing;
using System.IO;
namespace ExamSystem.Common
{
	/// <summary>
	/// 图片和字符数组转换的类
	/// </summary>
	public class PictureMan
	{
		public PictureMan()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}
		
		/// <summary>
		/// 读取本地图片
		/// </summary>
		/// <param name="root">图片路径</param>
		/// <param name="width">读出来的图片宽</param>
		/// <param name="height">读出来的图片高</param>
		/// <returns></returns>
		public static Image loadImage(string root,int width,int height)
		{
			if(!File.Exists(root))
			{
				return null;
			}
			Bitmap bmpSource=new Bitmap(root);
			Bitmap bmpNew=new Bitmap((Image)bmpSource,new Size(width,height));
			bmpSource.Dispose();
			return (Image)bmpNew;
		}

		/// <summary>
		/// 把字节数组转成Image
		/// </summary>
		/// <param name="byteImage">要转换的字节数组</param>
		/// <returns></returns>
		public static Image getImage(byte[] byteImage)
		{
			if(byteImage==null)
			{
				return null;
			}
			Bitmap bmp=null;
			try
			{
				MemoryStream stm=new MemoryStream(byteImage);
				bmp=new Bitmap(stm);
			}
			catch{}
			return (Image)bmp;
		}
		
		/// <summary>
		/// 把Image转成字节数组
		/// </summary>
		/// <param name="img">要转换的Image</param>
		/// <param name="width">转换后的图片宽</param>
		/// <param name="height">转换后的图片高</param>
		/// <returns></returns>
		public static byte[] getBytes(Image img)
		{
			byte[] imgBytes=null;
			try
			{
				MemoryStream stm=new MemoryStream();
				img.Save(stm,System.Drawing.Imaging.ImageFormat.Jpeg);
				imgBytes=new byte[stm.Length];
				stm.Position=0;
				stm.Write(imgBytes,0,imgBytes.Length);
				stm.Flush();
				stm.Close();
				img.Dispose();
			}
			catch{}
			return imgBytes;

		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?