从DB加载后获取黑色图像 [英] Getting black image after loading it from DB
问题描述
经过一段时间的尝试并在这里寻找答案,我无法从MySQL加载图像。
After quite some time of trying and looking arround here for some answers, i am unable to load an image from MySQL.
基本上,用户从中选择图片和 OpenFileDialog
然后加载到 PictureBox
(这样可以正常工作)。
然后用户单击一个按钮,将图片保存在数据库中,方法是将其转换为 byte []
。
最后,当我尝试加载图片时在另一个 PictureBox
中,它全是黑色。
Basicly, a user choses a picture from and OpenFileDialog
which is then loaded in a PictureBox
(this works fine).
Then the user clicks a button which will save the picture in the db by converting it to a byte[]
.
Finally, when I try to load the picture in another PictureBox
, it's all black.
将图片添加到数据库时:
When adding the picture to the db:
public void PictureToDB(Image img)
{
MemoryStream tmpStream = new MemoryStream();
img.Save(tmpStream, ImageFormat.Png);
tmpStream.Seek(0, SeekOrigin.Begin);
byte[] imgBytes = new byte[5000];
tmpStream.Read(imgBytes, 0, 5000);
string query = "Update TABLE Set IMG = @imgBytes";
MySqlParameter param = new MySqlParameter("@img", imgBytes);
//Skipping connection to db and all... it works
}
从db获取图片:
public void DBToPicture()
{
string query = "Select IMG From TABLE Where ...";
//Skipping Command lines ...
MySqlDataReader reader = command.ExecuteReader();
DataTable myDT.Load(reader);
Byte[] data = new Byte[0];
data = (Byte[])(myDT.Rows[0]["PHOTOLOISANT"]);
MemoryStream mem = new MemoryStream(data);
MyPictureBox.Image = Image.FromStream(mem);
}
更多信息:
- MySQL中的类型是
varbinary(8000)
并且在<$ c $之后包含这个89504e470d0a1a0000000d40048445200000280000001e00802000000bab34bb3000000043524144200ece1ce90000000467414d41002e2e2e c>更新 - 当图像在第一个
PictureBox
中加载时,图像将转换为位图。
- the type in MySQL is
varbinary(8000)
and contains this "89504e470d0a1a0a0000000d4948445200000280000001e00802000000bab34bb3000000017352474200aece1ce90000000467414d41002e2e2e" after theupdate
- the image is converted to bitmap when it is loaded in the first
PictureBox
.
推荐答案
用于存储:
conn = new MySqlConnection("server=" + hostname + ";uid=" + username + ";pwd=" + password + ";database=databaseimage;Charset=latin1;");
conn.Open();
FileStream fs;
Byte[] bindata;
MySqlParameter picpara;
cmd = new MySqlCommand("INSERT INTO mypic (pic) VALUES(?pic)", conn);
picpara = cmd.Parameters.Add("?pic", MySqlDbType.MediumBlob);
cmd.Prepare();
//txtPicPath is the path of the image, e.g. C:\MyPic.png
fs = new FileStream(txtPicPath.Text, FileMode.Open, FileAccess.Read);
bindata = new byte[Convert.ToInt32(fs.Length)];
fs.Read(bindata, 0, Convert.ToInt32(fs.Length));
fs.Close();
picpara.Value = bindata;
cmd.ExecuteNonQuery();
要检索它:
if (conn == null) // Just to make sure that the connection was not severed
{
conn = new MySqlConnection("server=" + hostname + ";uid=" + username + ";pwd=" + password + ";database=databaseimage;Charset=latin1;");
conn.Open();
}
MemoryStream ms = new MemoryStream();
FileStream fs;
Byte[] bindata;
cmd = new MySqlCommand("SELECT pic FROM mypic WHERE id=3", conn);
bindata = (byte[])(cmd.ExecuteScalar());
ms.Write(bindata, 0, bindata.Length);
pb2.Image = new Bitmap(ms);
fs = new FileStream(name, FileMode.Create, FileAccess.Write);
ms.WriteTo(fs);
我将使用这些步骤来存储和检索MySql中的图像。
I will use these kinda steps to store and retrieve image from MySql.
此致,
Thiyagu Rajendran
Thiyagu Rajendran
* *如果答案有帮助,请将答复标记为答案,如果答案没有,请将其取消标记。
**Please mark the replies as answers if they help and unmark if they don't.
这篇关于从DB加载后获取黑色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!