How to save picture in the database - vb.net

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()

Related

Show image in PictureBox from DB

When I retrieve an object from my DB, I try to do this:
Dim pictureBytes As New MemoryStream(imagen.JugFir)
pictureBox2.Image = Image.FromStream(pictureBytes)
But I get an error:
'imagen.JugFir' is an Object and Image.FromStream() requires a Byte[].
How i can convert Object to Byte[] ?
Or is there any other way to load the picture?

Converting Ink to Base64 and then to Image gets shapes cropped

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.

Display picture from access and show on form in vb.net

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.

WP8 - Display the photo i have taken in the control Image

Title explains everything.
How should i do ?
I coded this :
Dim size As Windows.Foundation.Size
Dim seq As CameraCaptureSequence
Dim imageStream As New MemoryStream
If PhotoCaptureDevice.AvailableSensorLocations.Contains(CameraSensorLocation.Back) Then
Dim avalaibleSizeList As IReadOnlyList(Of Windows.Foundation.Size) = PhotoCaptureDevice.GetAvailableCaptureResolutions(CameraSensorLocation.Back)
size = avalaibleSizeList(0)
Me.captureDevice = Await PhotoCaptureDevice.OpenAsync(CameraSensorLocation.Back, size)
End If
seq = captureDevice.CreateCaptureSequence(1)
seq.Frames(0).CaptureStream = imageStream.AsOutputStream()
Await captureDevice.PrepareCaptureSequenceAsync(seq)
Await seq.StartCaptureAsync()
imageStream.Seek(0, SeekOrigin.Begin)
Dim library As New MediaLibrary
Dim picture As Picture = library.SavePictureToCameraRoll("PhotosIncidents", imageStream)
But what i am suppose to do now?
You can create a BitmapImage, set it's source to your imageStream. Then put an Image control in XAML to display BitmapImage in your application :
Dim bitmapImage As BitmapImage
bitmapImage.SetSource(imageStream)
MyImageControl.Source = bitmapImage
Note : I havent tested the code, but thats what I will try if I need to do the same.

Image field on ReportViewer

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.