博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于pictureBox绑定图像,并保存图像到数据库进行读写
阅读量:4331 次
发布时间:2019-06-06

本文共 2393 字,大约阅读时间需要 7 分钟。

浏览:

string fileName = string.Empty; 

private void button1_Click(object sender, EventArgs e)

{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "*.BMP,*.JPG;*.GIF|*.BMP;*.JPG;*.GIF";
openFileDialog1.ShowDialog();
fileName = openFileDialog1.FileName;
Image a = Image.FromFile(openFileDialog1.FileName);
Bitmap bit = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(bit);//从指定的 Image 创建新的 Graphics(绘图)。
g.DrawImage(a, new Rectangle(0, 0, bit.Width, bit.Height), new Rectangle(0, 0, a.Width, a.Height), GraphicsUnit.Pixel);
//第一个参数:要绘制的图像
//第二个参数:它指定所绘制图像的位置和大小。 将图像进行缩放以适合该矩形。
//第三个参数:它指定 image 对象中要绘制的部分。
pictureBox1.Image = bit;
}
确定:上传到数据库
private void button2_Click(object sender, EventArgs e)
{
FileStream fs = File.OpenRead(fileName);
byte[] img = new byte[fs.Length];
fs.Read(img, 0,img.Length);
fs.Close();
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["Conn"].ToString());
SqlParameter p = new SqlParameter("@images", img);
SqlCommand sc = new SqlCommand("update Employees set employeeTx = @images where employeeID =2",conn);
sc.Parameters.Add(p);
if (sc.Connection.State == ConnectionState.Closed)
{
sc.Connection.Open();
}
sc.ExecuteNonQuery();
sc.Connection.Close();
Form3 f = new Form3();
this.Hide();
f.Show();
}

Form3窗体中显示图像 :

private void Form3_Load(object sender, EventArgs e)

{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["Conn"]);
SqlCommand com = new SqlCommand("select employeeTx from Employees where employeeID = 2",conn);
conn.Open();
byte[] b = (byte[])com.ExecuteScalar();
if (b.Length > 0)
{
MemoryStream s = new MemoryStream(b, true);
s.Write(b,0,b.Length);
Image a = new Bitmap(s);
Bitmap bit = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(bit);//从指定的 Image 创建新的 Graphics(绘图)。
g.DrawImage(a, new Rectangle(0, 0, bit.Width, bit.Height), new Rectangle(0, 0, a.Width, a.Height), GraphicsUnit.Pixel);
//第一个参数:要绘制的图像
//第二个参数:它指定所绘制图像的位置和大小。 将图像进行缩放以适合该矩形。
//第三个参数:它指定 image 对象中要绘制的部分。
pictureBox1.Image = bit;
}

 

另:绘制边框

private void pictureBox1_Paint(object sender, PaintEventArgs e)

{
  e.Graphics.DrawRectangle(new Pen(Color.Black, 2), new Rectangle(new Point(1, 1), new Size(this.pictureBox1.Width - 2, this.pictureBox1.Height - 2))); //考虑到线的宽度为4

转载于:https://www.cnblogs.com/zhcw/archive/2011/10/10/2205672.html

你可能感兴趣的文章
BABOK - 需求分析(Requirements Analysis)概述
查看>>
第43条:掌握GCD及操作队列的使用时机
查看>>
Windows autoKeras的下载与安装连接
查看>>
CMU Bomblab 答案
查看>>
微信支付之异步通知签名错误
查看>>
2016 - 1 -17 GCD学习总结
查看>>
linux安装php-redis扩展(转)
查看>>
Vue集成微信开发趟坑:公众号以及JSSDK相关
查看>>
技术分析淘宝的超卖宝贝
查看>>
i++和++1
查看>>
react.js
查看>>
P1313 计算系数
查看>>
NSString的长度比较方法(一)
查看>>
Azure云服务托管恶意软件
查看>>
My安卓知识6--关于把项目从androidstudio工程转成eclipse工程并导成jar包
查看>>
旧的起点(开园说明)
查看>>
生产订单“生产线别”带入生产入库单
查看>>
crontab导致磁盘空间满问题的解决
查看>>
java基础 第十一章(多态、抽象类、接口、包装类、String)
查看>>
Hadoop 服务器配置的副本数量 管不了客户端
查看>>