Anybody tried to display image field (image/byte array) data type on reportviewer.
Regards,
Peter
Yes. ReportViewer requires a Base64 Image encoding in order to display the image properly.
If your image is in a Byte array, it will need to be converted to Base64:
Public Function ConvertImageToBase64String(ByVal img As Image) As String
Dim output As String = ""
Dim outputArray() As Byte
Dim stream As New MemoryStream
img.Save(stream, Drawing.Imaging.ImageFormat.Bmp)
outputArray = stream.ToArray()
stream.Close()
output = Convert.ToBase64String(outputArray)
Return output
End Function
I have with an image data type in SQL Server. Works fine with SSRS 2005 and 2008.
Related
I am using the function below to convert some ink shapes I have drawn on a picturebox control to Base64
Function GetBase64(ByVal InkCollector As InkCollector) As String
Dim utf8 As UTF8Encoding = New UTF8Encoding()
InkCollector.Enabled = False
Return utf8.GetString(InkCollector.Ink.Save(PersistenceFormat.Base64InkSerializedFormat, CompressionMode.Maximum))
InkCollector.Enabled = True
End Function
I can then use the string to reproduce the sketch into a new image by feeding it to this function:
Function Base64toImage(ByVal Base64 As String) As System.Drawing.Image
Dim utf8 As UTF8Encoding = New UTF8Encoding()
Dim imgSig As System.Drawing.Image
Dim tmploadedInk As Ink = New Ink()
Dim strGIF As String
Dim imageBytes() As Byte
Dim MS As MemoryStream
'Load the Base64 String in Format(PersistenceFormat = Base64InkSerializedFormat) as ink
tmploadedInk.Load(utf8.GetBytes(Base64))
'Convert the ink to Base64 String in format (PersistenceFormat.Gif, CompressionMode.Maximum)
strGIF = Convert.ToBase64String(tmploadedInk.Save(PersistenceFormat.Gif, CompressionMode.Maximum))
'Convert Base64 String to Byte Array
imageBytes = Convert.FromBase64String(strGIF)
MS = New MemoryStream(imageBytes, 0, imageBytes.Length)
' Convert byte[] to Image
MS.Write(imageBytes, 0, imageBytes.Length)
imgSig = Image.FromStream(MS, True)
Return imgSig
End Function
This works fine except for the fact the shapes are cropped and therefore they get aligned to the top left corner of the generated image. If I use a line of code like below:
PictureBox1.Image = Signiture.Base64toImage(Signiture.GetBase64(Sketch_InkCollector))
The shapes appear at the top left and ignore the fact that they might have been drawn at the center of the original picture box. I believe that the positioning data is being properly saved into the Base64 string, but are somehow being ignored when the shapes are transferred to the image variable. The reason I believe this is because when I use the function below with the Base64 string, the shapes get placed properly in their original position.
Sub SetBase64PictureBox(ByVal InkCollector As InkCollector, ByVal PictureBox As PictureBox, ByVal Base64 As String)
Dim loadedInk As Ink = New Ink()
Dim utf8 As UTF8Encoding = New UTF8Encoding()
InkCollector.Enabled = False
InkCollector.Ink.DeleteStrokes() ' Clear all strokes
loadedInk.Load(utf8.GetBytes(Base64))
InkCollector.Ink = loadedInk
InkCollector.Enabled = True
PictureBox.Invalidate()
End Sub
Thank you
Update:
Found a temporary solution in this link:
InkPicture control - How to save both ink and image to an image file
It is not exactly what I was after but the end result is the same since I wanted to the combined picture and the ink strokes into an image to show in a PDF file.
I want to display picture from access and show that on a form in vb.net
I have this for display information:
info.TextBox5.Text = DataGridView1.SelectedRows(0).Cells(5).Value
Now I tried something like this for pictures:
info.PictureBox1.Image = DataGridView1.SelectedRows(0).Cells(6).Value
But I got an error:
Can not associate the type of Object ' System.Byte []' to type '
System.Drawing.Image ' .
Can you help me?
dim str as string =DataGridView1.SelectedRows(0).Cells(5).Value
if str <> string.empty then
info.TextBox5.Text = str
end if
Use this:
Using ms As New MemoryStream(CType(DataGridView1.SelectedRows(0).Cells(6).Value, Byte()))
info.PictureBox1.Image = New Bitmap(ms)
End Using
Since you cannot simply assign a Byte() to an Image, you need to first create a Bitmap out of the data.
So what the above code does is:
Creates a new MemoryStream with the Byte() returned by DataGridView1.SelectedRows(0).Cells(6).Value.
Initializes a new Bitmap with the data in that stream, so that you can assign it to the Image property of your PictureBox.
I wrote the follow function to convert an image to bytes:
Public Function ConvertImage(ByVal myImage As Image) As Byte()
'store image in memory before converting and
'create memory stream, save image in proper format
Dim mStream As New MemoryStream
myImage.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg)
'convert new stream into bytes and indicate size
Dim myBytes(mStream.Length - 1) As Byte
mStream.Position = 0
mStream.Read(myBytes, 0, mStream.Length)
Return myBytes
End Function
The function works great, however, I am struggling in trying to figure out how to best modify my function to also check if an image exist. if an image exists, then convert, if no image exists, then return Nothing from a particular path and then convert. Can I do it on this function, or do I have to write another function?
I am using following code to save my data to database
Where does the statement to save a picture in the database fit?
Dim drNewRowMCQsAns As DataRow
drNewRowMCQsAns = DsResultSaveNow.tblResult.NewRow
drNewRowMCQsAns.Item("PaperID") = vrPaperIDInitialized
drNewRowMCQsAns.Item("StudentID") = vrStudentID
drNewRowMCQsAns.Item("StudentName") = vrStudentName
DsResultSaveNow.tblResult.Rows.Add(drNewRowMCQsAns)
taResultSaveNow.Update(DsResultSaveNow.tblResult)
I have image field in the database but how to save an image?
Thanks
Well, the image data is simply a byte array
Dim imageData as Byte()
Load your image into the byte array from where ever you are getting it from and set it just like the other properties
drNewRowMCQsAns.Item("ImageData") = imageData
Loading image into array:
From file:
imageData = IO.File.ReadAllBytes("c:\filename.jpg")
From bitmap:
Dim bitmap As New System.Drawing.Bitmap("c:\filename.jpg")
Dim tempMemStream As New IO.MemoryStream
bitmap.Save(tempMemStream, System.Drawing.Imaging.ImageFormat.Jpeg)
imageData = tempMemStream.ToArray()
I am trying to retrieve an image from an Embedded resource, and displaying it in its RAW DATA format (i.e. -> junk text data).
Basically, I am running into a wall with everything I attempt.
Can someone show me how to do this properly?
You can use the following msdn ref
System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(fileName)
That will give you a Stream which you can then use code cite to convert to a byte array which is pretty much the raw data.
private Function GetStreamAsByteArray(ByVal stream As System.IO.Stream) As Byte()
Dim streamLength As Integer = Convert.ToInt32(stream.Length)
Dim fileData As Byte() = New Byte(streamLength) {}
' Read the file into a byte array
stream.Read(fileData, 0, streamLength)
stream.Close()
Return fileData
End Function