C#的WINFORM开发中上传图片转二进制再转图片展示的方法
建立一个窗体HdhCmsListImageForm,上面有个图片预览框picbox1,有一个btnimg按钮,默认文字为“选择”。
public partial class HdhCmsListImageForm : Form
{
private static byte[] _imgdata = null; //保存图片二进制私有变量
/// <summary>
/// 图片操作,有一个按钮未选图片为“选择”,选了图片显示“移除”
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnimg_Click(object sender, EventArgs e)
{
if (btnimg.Text == "选择")
{
#region 图片转二进制-001
Image _img = Img.LoadImg();
if (_img != null)
{
btnimg.Text = "移除";
picbox1.Image = _img;
byte[] imageBytes;
using (MemoryStream ms = new MemoryStream())
{
_img.Save(ms, ImageFormat.Jpeg);
_imgdata = ms.ToArray();
}
}
#endregion
}
else if (btnimg.Text == "移除")
{
InitImg();
}
}
//初始化数据
private void InitHdhcms()
{
_imgdata = null;
btnimg.Text = "选择";
//默认图片
picbox1.Image = Img.noimg();
}
/// <summary>
/// 二进度转图片显示到窗口_hdhcms.imgdata保存的是图片二进制代码
/// </summary>
private void HdhCmsByteToImage(int id, string name, string code)
{
if (_hdhcms.imgdata.imgdata != null)
{
_imgdata = (byte[])_hdhcms.imgdata;
using (MemoryStream ms = new MemoryStream(_pfd.imgdata))
{
Image image = Image.FromStream(ms);
picbox1.Image = image;
}
}
}
}
图片操作类:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HdhCmsImageClass
{
/// <summary>
/// 图片操作类
/// </summary>
public class Img
{
public static string path = Application.StartupPath.Substring(0, Application.StartupPath.IndexOf("HLTKQUOTATION"));
/// <summary>
/// 加载图片
/// </summary>
public static Image LoadImg()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "请选择图片";
ofd.InitialDirectory = Path.GetDirectoryName(ofd.FileName);
ofd.Filter = "(*.jpg/png)|*.jpg;*.png";//只允许.jpg或.png
ofd.Multiselect = false;//true允许上传多张图片
//当用户点击打开时,进入操作逻辑
if (ofd.ShowDialog() == DialogResult.OK)
{
string path = ofd.FileName;
Console.WriteLine(path);
//创建FileStream读取图片流
using (FileStream fileRead = new FileStream(@path, FileMode.OpenOrCreate, FileAccess.Read))
{
byte[] buffer = new byte[fileRead.Length];
fileRead.Read(buffer);
Image i = Image.FromStream(fileRead);
return i;
}
}
else {
return null;
}
}
/// <summary>
/// 保存图片,返回路径
/// </summary>
/// <param name="picbox1"></param>
/// <returns></returns>
public static string SaveImg(PictureBox picbox1)
{
try
{
Bitmap bit = new Bitmap(picbox1.Width, picbox1.Height);
picbox1.DrawToBitmap(bit, picbox1.ClientRectangle);
string fileExt = ".png";
string fileName = DateTime.Now.ToString("mmssffff")+ fileExt; //文件名
string relPath = Path.Combine("Upload", DateTime.Now.ToString("yyMMdd"), fileName); //拼接相对路径
string fullPath = Path.Combine(path, relPath); //拼接绝对路径
string filePath = Path.Combine(path, "Upload", DateTime.Now.ToString("yyMMdd"));//文件路径
if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);
bit.Save(fullPath, System.Drawing.Imaging.ImageFormat.Bmp);
MessageBox.Show("上传成功");
return relPath;
}
catch {
MessageBox.Show("上传异常");
return "";
}
}
/// <summary>
/// 默认图片
/// </summary>
public static Image noimg() {
string path2 = "Upload\\noimg.png";
string url = path + path2;
return Image.FromFile(url);
}
}
}
查看更多关于C#的WINFORM开发中上传图片转二进制再转图片展示的方法的详细内容...