如何使用单击单击将图像显示回图片框? [英] How to display the image back to picture box using cell click?

查看:66
本文介绍了如何使用单击单击将图像显示回图片框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用来自88388132的代码 - @ User-9325517,如何使用数据网格视图的事件 - 单元格点击将图像显示回图片框?



using the code from 88388132 - @User-9325517 , how to display the image back to picture box using data grid view's event - cell click?

 SqlConnection cn = new SqlConnection("data source=localhost;initial catalog=notitest;user id=sa;password=cos123");
  cn.Open();
  SqlCommand cmd = new SqlCommand("insert imgtest values(" + textBox1.Text + ",'" + ImageToBase64(pictureBox1.Image,
System.Drawing.Imaging.ImageFormat.Png) + "')", cn);
  SqlDataReader dr = cmd.ExecuteReader();
  cn.Close();


//Retrive
SqlConnection cn = new SqlConnection("data source=localhost;initial catalog=notitest;user id=sa;password=cos123");
cn.Open();
SqlCommand cmd = new SqlCommand("select * from imgtest", cn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
    textBox2.Text = dr[0].ToString();
    pictureBox2.Image = Base64ToImage(dr[1].ToString());
}
cn.Close();



//Methods
Hide   Copy Code
public string ImageToBase64(Image image,
          System.Drawing.Imaging.ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                // Convert Image to byte[]
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();
 
                // Convert byte[] to Base64 String
                string base64String = Convert.ToBase64String(imageBytes);
                return base64String;
            }
        }
 
        public Image Base64ToImage(string base64String)
        {
            // Convert Base64 String to byte[]
            byte[] imageBytes = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(imageBytes, 0,
              imageBytes.Length);
 
            // Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length);
            Image image = Image.FromStream(ms, true);
            return image;
        }





我的尝试:



试过这个但是我收到一个错误,说base-64 char数组或字符串的长度无效。我将字符串保存到sql server中的文本字段/数据类型,并将其更改为nvarchar或任何其他数据类型不是一个选项。



What I have tried:

Tried this but i got an error saying "invalid length for a base-64 char array or string". I am saving the string into text field/data type in sql server and changing it to nvarchar or any other data types isn't an option.

SqlConnection cn = new SqlConnection("data source=localhost;initial catalog=notitest;user id=sa;password=cos123");
cn.Open();
SqlCommand cmd = new SqlCommand("select * from imgtest", cn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
    textBox2.Text = dr[0].ToString();
    pictureBox2.Image = Base64ToImage(dr[1].ToString());
}
cn.Close();

推荐答案

猜测,数据库中的数据不是您的想法是:为什么我得到一个参数无效。我从数据库中读取图像时出现异常? [ ^ ]将解释原因。


和其他人说的一样:永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

At a guess, the data in your DB is not what you think it is: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] will explain why.

And as the others have said: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?


这篇关于如何使用单击单击将图像显示回图片框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆