所以我在 tabcontrol 中有幾個選項卡,它們執行注冊和修改有關客戶端的資訊的任務。其他資訊中的第一個,使用 OpenFileDialog 保存圖片,然后將其存盤到 'lblImagePath.Text' 以使用位圖,以便我可以將其保存到 de DB 中,可以在以下代碼中看到:
public partial class form : Form
{
String strDBFilePath = "";
String strFilePath = "";
Image DefaultImage;
byte[] ImageByteArray;
byte[] ImageByteArrayMod;
private void btnAddUser_Click(object sender, EventArgs e)
{
if (txtEmailPersonal.Text != "")
{
try
{
Image temp = new Bitmap(strFilePath);
MemoryStream ms = new MemoryStream();
temp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ImageByteArray = ms.ToArray();
SqlConnection con = new SqlConnection();
con.ConnectionString = CONEXION.CONEXIONMAESTRA.conexion;
con.Open();
SqlCommand cmd = new SqlCommand();
cmd = new SqlCommand("insert_user", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@email", txtEmailPersonal.Text);
cmd.Parameters.AddWithValue("@password", txtPasswordPersonal.Text);
cmd.Parameters.AddWithValue("@profile_picture", ImageByteArray);
cmd.Parameters.AddWithValue("@imageFile_name", lblImagePath.Text);
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
mostrar();
}
}
}
一切順利,注冊表作業正常,我正在使用 datagridview 對其進行可視化。當我雙擊 dgv 中的一行時,所有資訊都會加載到第二個選項卡中,讓我修改除個人資料圖片之外的所有資訊,可以在圖片框中預覽但不加載任何其他資訊,所以當我點擊“保存更改”按鈕,它不會讓我繼續它,直到我重新上傳個人資料圖片,因為在該操作之前路徑不存在。這是用戶修改的代碼:
private void btnGuardarCambiosPersonal_Click(object sender, EventArgs e)
{
if (txtEmailPersonalMod.Text != "")
{
try
{
Image temp = new Bitmap(strDBFilePath);
MemoryStream ms = new MemoryStream();
temp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ImageByteArrayMod = ms.ToArray();
SqlConnection con = new SqlConnection();
con.ConnectionString = CONEXION.CONEXIONMAESTRA.conexion;
con.Open();
SqlCommand cmd = new SqlCommand();
cmd = new SqlCommand("modify_user", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@correo", txtEmailPersonalMod.Text);
cmd.Parameters.AddWithValue("@password", txtPasswordPersonalMod.Text);
cmd.Parameters.AddWithValue("@profile_picture", ImageByteArrayMod);
cmd.Parameters.AddWithValue("@imageFile_name", lblFilePathMod.Text);
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
mostrar();
}
}
我在那里的一些東西實際上可能對完成我想要的東西毫無用處。所以我會盡量說清楚,如果沒有上傳其他個人資料圖片來替換它,我希望能夠保留當前的個人資料圖片。
正如您可能還看到的,我使用存盤程序而不是直接在代碼上執行查詢,其想法是保留這些存盤程序并嘗試在代碼中進行調整。
uj5u.com熱心網友回復:
閱讀@Llama 的評論后,我意識到解決方案非常簡單,在修改代碼的基礎上,我在 try/catch 的開頭添加了以下內容:
Image tempy = new Bitmap(picPerfilMod.Image);
MemoryStream mems = new MemoryStream();
tempy.Save(mems, System.Drawing.Imaging.ImageFormat.Jpeg);
ImageByteArrayMod = mems.ToArray();
所以我可以將圖片從圖片框轉回陣列,而無需修改它。
我將繼續閱讀有關varbinary這種情況下使用列型別的資訊,因為它顯然看起來是一個更好/更簡單的想法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/363190.html
