PDA

View Full Version : سوال: تبدیل آرایه به تصویر



visual_sadegh
پنج شنبه 09 آبان 1387, 13:44 عصر
من یک کد به این شکل نوشتم

Cmd.CommandText = "Select [Image] From Images Where ID=" & Me.bsImages.Current.Item("ID")
Dim Img = Cmd.ExecuteScalar
Me.PictureBox1.Image = Img
پیام میده که نمی تونه آرایه رو به عکس تبدیل کنه
باید چکار کنم
بعضی وقت ها هم نمی تونه یک عکس رو از پایگاه داده بخونه و پیغام کانکشن تایم اوت می ده

sepehr.net
پنج شنبه 09 آبان 1387, 20:03 عصر
سلام به خاطر اینکه نحوه نوشتنت اشتباه است . تو تصویر را به صورت باینری در بانک ذخیره کردی؟ یا ادرس اون رو ذخیره کردی؟
فکر کنم اگه از دیتا آداپتر و دیتا تیبل استفاده کنی به نتیجه میرسی

visual_sadegh
شنبه 11 آبان 1387, 10:40 صبح
کل تصویر رو در یک فیلد ذخیره کردم و img به یک آرایه از نوع بایت تبدیل میشه
حالا می خوام این آرایه از نوع بایت که محتوای اون یک تصویر هست رو توی پیکچر باکس نشون بدم
حالا باید چکار کنم

shask00l
شنبه 11 آبان 1387, 13:34 عصر
کل تصویر رو در یک فیلد ذخیره کردم و img به یک آرایه از نوع بایت تبدیل میشه
حالا می خوام این آرایه از نوع بایت که محتوای اون یک تصویر هست رو توی پیکچر باکس نشون بدم
حالا باید چکار کنم

خوب دوست عزیز . خودت داری جواب سوالت رو میدی . آرایه از نوع بایت رو میخای همینجوری بریزی توی picturebox ؟
بهتره اول آرایت رو با یک filestream روی یک فایل temp بنویسی بعد با picturebox بازش کنی.
البته میتونی همینجوری هم توی picbox نمایش بدی ولی دردسرش زیاده .

sh
شنبه 11 آبان 1387, 22:14 عصر
برای راه حل میتونی به لینکهای زیر نگاه کنی :

VB 2005 "converting images into binary (http://www.dreamincode.net/forums/showtopic51183.htm)

StoringImagesInVB (http://www.vbdotnetheaven.com/UploadFile/aghiondea2/StoringImagesInVB11122005034248AM/StoringImagesInVB.aspx?ArticleID=7ed1fe42-0819-4912-88ae-bbc43829d8a6)


یا کد :



' Add a reference to System.Configuration.dll to the project.

Imports System
Imports System.IO
Imports System.Drawing
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Drawing.Imaging

Public Class SqlServerImages

''' <summary>
''' Load an image from a file, and create a byte array using the image format type
''' (i.e. Bmp, Png, Jpeg, etc.)
''' </summary>
''' <param name="fileName">Full path file name for the image file.</param>
''' <param name="format">The image type (Bmp, Jpeg, Png, etc.)</param>
''' <returns>A byte array representing the image.</returns>
Public Function LoadImage(ByVal fileName As String, ByVal format As ImageFormat) As Byte()

Dim img As Image = Image.FromFile(fileName)
Using ms As New MemoryStream()
img.Save(ms, format)
Return ms.ToArray()
End Using
End Function

''' <summary>
''' Save the image byte array to the Image column in the SQL Server table, with ID and Name.
''' </summary>
''' <param name="id">The identity information for the image</param>
''' <param name="name">The human readable name for the image</param>
''' <param name="image">The byte array representing the image</param>
''' <returns>Number of rows affected</returns>
Public Function SaveImage(ByVal id As Integer, ByVal name As String, ByVal image As Byte()) As Integer

Dim connectionString As String = ConfigurationManager.ConnectionStrings("Connection").ConnectionString
Using connection As New SqlConnection(connectionString)
connection.Open()

Dim commandText As String = "INSERT INTO tblImgData(ID, Name, Picture) VALUES (@ID, @Name, @Picture)"

Using command As New SqlCommand(commandText, connection)
command.Parameters.AddWithValue("@ID", id)
command.Parameters.AddWithValue("@Name", name)
command.Parameters.AddWithValue("@Picture", image)

Return command.ExecuteNonQuery()
End Using
End Using
End Function

''' <summary>
''' Pull the image from the database with the given ID. Translate the byte array
''' into a image, and return an instance of an Image class.
''' </summary>
''' <param name="id">The identity information for the image.</param>
''' <returns>An Image instance</returns>
Public Function RetrieveImage(ByVal id As Integer) As Image
Dim connectionString As String = ConfigurationManager.ConnectionStrings("Connection").ConnectionString
Using connection As New SqlConnection(connectionString)
connection.Open()

Dim commandText As String = "SELECT Picture FROM tblImgData WHERE ID = @ID"

Using command As New SqlCommand(commandText, connection)
command.Parameters.AddWithValue("@ID", id)

Dim buffer As Byte() = command.ExecuteScalar()
Using ms As New MemoryStream(buffer)
Return Image.FromStream(ms)
End Using
End Using
End Using
End Function
End Class

visual_sadegh
یک شنبه 12 آبان 1387, 08:22 صبح
آقا شهریار ممنون
با راهنمایی شما جواب گرفتم کد را به این شکل تغییر دادم

Cmd.CommandText = "Select [Image] From Images Where ID=" & Me.bsImages.Current.Item("ID")
Dim Img() As Byte
Img = Cmd.ExecuteScalar
Dim Ms As New IO.MemoryStream(Img)
Me.PictureBox1.Image = Image.FromStream(Ms)