问题描述
我是初学者,对于我的要求感到抱歉,但我需要帮助和启发。所以这就是我提出的以及我从一篇成员文章中得到的内容,而我却无法理解它。所以这就是我所做的导致错误对象引用未设置为对象的实例。 :
我尝试过:
使用System.Data。 SqlClient;
使用System.Drawing.Imaging;
使用System.IO;
命名空间Kenny
{
公共部分类Form1:表格
{
SqlConnection con = new SqlConnection(Data Source =。;初始目录=主;集成安全性=真);
SqlCommand cmd;
SqlDataAdapter适应;
public Form1()
{
InitializeComponent();
}
private void buttonsave_Click(object sender,EventArgs e)
{
Image myImage = pictureBox1.Image;
byte [] data;
using(MemoryStream ms = new MemoryStream())
{
myImage.Save(ms,System.Drawing。 Imaging.I mageFormat.Bmp);
data = ms.ToArray();
}
con.Open( );
cmd = new SqlCommand(INSERT INTO table(picture)VALUES(@image),con);
{
cmd .Parameters.AddWithValue(@ image,data);
cmd.ExecuteNonQuery();
}
con.Close();
}
private void buttonbrowseimage_Click(object sender,EventArgs e)
{
//打开文件对话框
OpenFileDialog open = new OpenFileDialog();
//图片过滤器
open.Filter =Image Files (* .JPG; * .JPEG; * .gif和* .BMP)| * .JPG; * .JPEG; * .gif和* .bmp;
if(open.ShowDialog()== DialogResult.OK)
{
//在图片框中显示图片
图片myImage =新位图(open.FileName);
}
}
private void buttonview_Click(object sender,EventArgs e)
{
cmd = new SqlCommand(SELECT picture FROM kenny WHERE Id = @ id,con);
con.Open();
cmd.Parameters.AddWithValue(@ ID,pictureBox1.Image);
SqlDataReader reader = cmd.ExecuteReader();
if(reader.Read())
{
byte [] data =(byte [])reader [Picture];
using(MemoryStream stream = new MemoryStream())
{
myImage =新的位图(流);
}
}
}
}
}
我所拥有的:响应浏览按钮的图片框,还应该在查看或加载时显示图像。保存按钮,用于将其保存到数据库。
I'm a beginner and sorry for what I am asking but I need help and enlightenment. So this is what I come up and what I got from a members article, and i'm having trouble understanding it. So this is what I made that causes error "Object reference not set to an instance of an object." :
What I have tried:
using System.Data.SqlClient;
using System.Drawing.Imaging;
using System.IO;
namespace Kenny
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
SqlCommand cmd;
SqlDataAdapter adapt;
public Form1()
{
InitializeComponent();
}
private void buttonsave_Click(object sender, EventArgs e)
{
Image myImage = pictureBox1.Image;
byte[] data;
using (MemoryStream ms = new MemoryStream())
{
myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
data = ms.ToArray();
}
con.Open();
cmd = new SqlCommand("INSERT INTO table(picture) VALUES (@image)", con);
{
cmd.Parameters.AddWithValue("@image", data);
cmd.ExecuteNonQuery();
}
con.Close();
}
private void buttonbrowseimage_Click(object sender, EventArgs e)
{
// open file dialog
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
// display image in picture box
Image myImage = new Bitmap(open.FileName);
}
}
private void buttonview_Click(object sender, EventArgs e)
{
cmd = new SqlCommand("SELECT picture FROM kenny WHERE Id=@id", con);
con.Open();
cmd.Parameters.AddWithValue("@ID", pictureBox1.Image);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
byte[] data = (byte[])reader["Picture"];
using (MemoryStream stream = new MemoryStream())
{
myImage = new Bitmap(stream);
}
}
}
}
}
what i have: picture box that responds to browse button and should also display image when view or loaded. save button for saving it to the DB.
推荐答案
using (MemoryStream stream = new MemoryStream())
{
myImage = new Bitmap(stream);
}
您没有将流设置为任何数据,因此没有图像...
尝试使用数据。
You don't set the stream to any data, so there is no image...
Try using data
in the MemoryStream constructor.
这篇关于图像到字节到DB到图像到图像框C#SQL服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!