با سلام
اول یک picture box روی فرم قرار بده و یک openfiledialog به فرم اضافه کن
بعد یک دکمه روی فرم بذارید و فرم زیر رو توی اون بنویسید
string s = Environment.CurrentDirectory;
ofd.Filter = "JPEG (*.JPG) | *.jpg|" + "Bitmap Files (*.bmp) | *.bmp";
if (ofd.ShowDialog() == DialogResult.OK)
{
strBK = ofd.FileName;
pic_BK.Image = System.Drawing.Bitmap.FromFile(strBK);
}
Environment.CurrentDirectory = s;
من یک تابع دارم با تعریف زیر :
public void INSERT_Picture_BK(string _ConnectionString, string path, string _FormName)
{
MyFunction MF = new MyFunction();
objConnection.ConnectionString = _ConnectionString;
objCommand.Connection = objConnection;
string strFileStream = string.Empty;
strFileStream = path;
if (strFileStream != null && strFileStream != "")
{
FileStream stream = new FileStream(strFileStream, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(stream);
byte[] photo = reader.ReadBytes((int)stream.Length);
reader.Close();
stream.Close();
objCommand.Parameters.Add("@Pic", SqlDbType.VarBinary, photo.Length).Value = photo;
}
else
{
objCommand.Parameters.Add("@Pic", SqlDbType.VarBinary).Value = DBNull.Value;
}
objCommand.CommandText = string.Format("UPDATE Pic_Main SET MainPic = @Pic WHERE Index_ = {0}", 0);
try
{
objConnection.Open();
objCommand.ExecuteNonQuery();
}
catch (SqlException error)
{
MessageBox.Show("درخواست مورد نظر قابل اجرا نمی باشد ، لطفاً بعداً اقدام نمایید", "اخطار", MessageBoxButtons.OK, MessageBoxIcon.Error);
MF.WriteError(error.Number, error.Message, _FormName, "SELECT_Picture_BK");
}
objConnection.Close();
}
این تابع رو با دستور زیر فراخوانی می کنم :
INSERT_Picture_BK(_ConnectionString, strBK, _FormName);
توی دیتابیس هم یک فیلد از نوع زیر تعریف کنید
varbinary(MAX)