如何将资源中的图像插入数据库 [英] How do I insert a image from resources to a db
问题描述
我正在创建一个配置文件,在一个表单上我有信息以及通过blob从数据库读取的图像
在另一个表单上是编辑他们可以更改信息的位置,并更改图片或选择恢复到项目属性中资源中的默认图像。
它保存并且加载很好但我的问题是如何使它如此,如果chk一个框(默认图像)它将转到默认图像
这是代码保存按钮的第二个表格
表格2
I am creating a profile and on one form I have information along with a image that is reading from the db via blob
on another form is a edit where they can change information and either change the pic or choose to revert to the default image which is in the resources in the properties of the project.
it saves and loads fine but my problem is how do I make it so it will go to the default image if the chk a box(default image)
Here is the code for the 2nd form for the save button
FORM 2
private void btnSave_Click(object sender, EventArgs e)
{
string name = this.txtname.Text;
string age = this.txtage.Text;
string country = this.txtcountry.Text;
string m_status = this.cmb_mar_status.Text;
string gender = this.cmbgender.Text;
string hobbies = this.txthobbies.Text;
byte[] imageBt = null;
FileStream fstream = new FileStream(this.path.Text, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fstream);
imageBt = br.ReadBytes((int)fstream.Length);
string q = ("update users set full_name = @name , age = @age, country = @country, martial_status =@m_status, gender=@gender,hobbies =@hobbies,profile_pic = @IMG WHERE username='" + label1.Text + "'");
string conString = "HIDDEN";
MySqlConnection mcon = new MySqlConnection(conString);
MySqlCommand cmdDataBase = new MySqlCommand(q, mcon);
MySqlDataReader myReader;
try
{
mcon.Open();
cmdDataBase.Parameters.Add(new MySqlParameter("@name", name));
cmdDataBase.Parameters.Add(new MySqlParameter("@age", age));
cmdDataBase.Parameters.Add(new MySqlParameter("@country", country));
cmdDataBase.Parameters.Add(new MySqlParameter("@m_status", m_status));
cmdDataBase.Parameters.Add(new MySqlParameter("@gender", gender));
cmdDataBase.Parameters.Add(new MySqlParameter("@hobbies", hobbies));
cmdDataBase.Parameters.Add(new MySqlParameter("@IMG", imageBt));
myReader = cmdDataBase.ExecuteReader();
MessageBox.Show("Saved");
while (myReader.Read())
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this.Close();
}
再次..我需要它保存如果他们选择显示默认图像,则将默认图像放入数据库。
Again.. I need to have it save the default image into the db if they have chosen to display the default image.
推荐答案
1。如果用户选择恢复默认值,则从目录中获取默认图像并将其转换为byte []
。然后触发UPDATE
命令。
1. If user choose to revert the default the value, you get the default image from the directory and convert it tobyte[]
. And then fire theUPDATE
command.
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
2.或者您只需将图像数据更新为 NULL
,并在表格上如果 Image为NULL
并从目录加载默认图像的条件。
希望我能帮到你。
-KR
2. Or you can simply update the Image data as NULL
, and on form put a condition that if Image is NULL
and load the default image from directory
.
Hope I got you correctly.
-KR
这篇关于如何将资源中的图像插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!