📄 pictureman.cs
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -