PDA

View Full Version : بایند کردن پیکچر باکس



pourya_comphard
جمعه 11 دی 1388, 14:06 عصر
با سلام
من تو دیتا بیسم عکسی را ذخیره کردم
حالا چطوری میتونم به پیکچر باکس بایندش کنم؟
مرسی

H2K
جمعه 11 دی 1388, 16:05 عصر
The following code can help you.


----this method shows u how to store images into database----


Public Sub StoreImage(ByVal con As SqlConnection, ByVal code As String, ByVal filename As String)





' CREATE AN INSERT COMMAND


Dim cmd As New SqlCommand("INSERT INTO IMAGES(CODE, DATA)VALUES(@CODE, @DATA)", con)


' CREATE PARAMETER FOR STORING SOME IDENTITY, YOU CAN HAVE SOME MORE OF YOUR


' FIELDS


Dim paramCode As SqlParameter = cmd.Parameters.Add("@CODE", SqlDbType.VarChar, 16)


' CREATE PARAMETER FOR STORING IMAGE DATA,


Dim paramData As SqlParameter = cmd.Parameters.Add("@DATA", SqlDbType.Image)


' ASSIGN FIELDS VALUES


paramCode.Value = code


' ASSIGN IMAGE DATA VALUE


paramData.Value = GetFileData(filename)


' EXECUTE THE QUERY


cmd.ExecuteNonQuery()


End Sub


Public Function GetFileData(ByVal FileName As String) As Byte()


' OPEN THE FILE FOR READING


Dim fs As New System.IO.FileStream(FileName, IO.FileMode.Open, IO.FileAccess.Read)


' ALLOCATE MEMORY TO STORE THE FILE DATA


Dim bytData As Byte() = New Byte(fs.Length - 1) {}


' READ FILE DATA AND STORE IT TO BYTE ARRAY


fs.Read(bytData, 0, bytData.Length)


' CLOSE THE FILE


fs.Close()


Return bytData


End Function



Here you need to have the Image datatype to store the Binary Image data


The second function GetFileData returns the entire file content as an Byte Array.
--------------------
to bind the image back to PictureBox, it can be as easy as a single line of code:


this.pictureBox2.DataBindings.Add("Image", dataset.Tables[0], "picture",true);



-----------------------------
To store image paths in the database as you currently do. In this scenario, we have to handle the Format event while binding the PictureBox:



private void Form5_Load(object sender, EventArgs e)


{


DataTable dt = ds.Tables[0];


Binding binding = new Binding("Image", dt, "pic",true);


binding.Format += new ConvertEventHandler(binding_Format);


this.pictureBox1.DataBindings.Add(binding);


}




void binding_Format(object sender, ConvertEventArgs e)


{


string path = (string)e.Value;


e.Value = Image.FromFile(path);


}


hope this will help u
:لبخندساده: