开发框架处理保存图片数据
开发框架处理保存图片数据
1.SQL数据库字段类型为Image。
2. 图片显示及编辑使用PictureEdit组件。PictureStoreMode设为ByteArray(存储模式设为字节数组)
3. ORM的字段类型定义为SqlDbType.Image,长度可以设大点。
4. 给PictureEdit绑定一个弹出菜单ContextMenuStrip,由用户加载或清空图片。
扫一扫加微信
1.SQL数据库字段类型为Image。
2. 图片显示及编辑使用PictureEdit组件。PictureStoreMode设为ByteArray(存储模式设为字节数组)
3. ORM的字段类型定义为SqlDbType.Image,长度可以设大点。
C# Code:
//最大支持20MB的图片
[FieldAttribute(SqlDbType.Image, 20480000, false, true, false, false, false)]
public static string PhotoImg = "PhotoImg";
//最大支持20MB的图片
[FieldAttribute(SqlDbType.Image, 20480000, false, true, false, false, false)]
public static string PhotoImg = "PhotoImg";
4. 给PictureEdit绑定一个弹出菜单ContextMenuStrip,由用户加载或清空图片。
C# Code:
private void menuLoad_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "图片文件(*.jpg,*.png,*.bmp)|*.jpg;*.png;*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
DataRow row = _CurrentBusiness.Tables[0].Rows[0];
Image img = Image.FromFile(dlg.FileName);
pictureEdit1.Image = CImageLibrary.ResizeImage(img, 640, 640);
row[TJLZB.PhotoImg] = CImageLibrary.GetImageBytes(pictureEdit1.Image);
}
}
private void menuClear_Click(object sender, EventArgs e)
{
DataRow row = _CurrentBusiness.Tables[0].Rows[0];
row[TJLZB.PhotoImg] = GetImageByte();//初始化空照片
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
menuLoad.Enabled = this.IsAddOrEditMode;
menuClear.Enabled = this.IsAddOrEditMode;
}
/// <summary>
/// 空照片
/// </summary>
/// <returns></returns>
private byte[] GetImageByte()
{
System.IO.MemoryStream Ms = new MemoryStream();
picTemp.Image.Save(Ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] img = new byte[Ms.Length];
Ms.Position = 0;
Ms.Read(img, 0, Convert.ToInt32(Ms.Length));
Ms.Close();
return img;
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
private void menuLoad_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "图片文件(*.jpg,*.png,*.bmp)|*.jpg;*.png;*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
DataRow row = _CurrentBusiness.Tables[0].Rows[0];
Image img = Image.FromFile(dlg.FileName);
pictureEdit1.Image = CImageLibrary.ResizeImage(img, 640, 640);
row[TJLZB.PhotoImg] = CImageLibrary.GetImageBytes(pictureEdit1.Image);
}
}
private void menuClear_Click(object sender, EventArgs e)
{
DataRow row = _CurrentBusiness.Tables[0].Rows[0];
row[TJLZB.PhotoImg] = GetImageByte();//初始化空照片
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
menuLoad.Enabled = this.IsAddOrEditMode;
menuClear.Enabled = this.IsAddOrEditMode;
}
/// <summary>
/// 空照片
/// </summary>
/// <returns></returns>
private byte[] GetImageByte()
{
System.IO.MemoryStream Ms = new MemoryStream();
picTemp.Image.Save(Ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] img = new byte[Ms.Length];
Ms.Position = 0;
Ms.Read(img, 0, Convert.ToInt32(Ms.Length));
Ms.Close();
return img;
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
扫一扫加微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网