WP8 - Display the photo i have taken in the control Image - vb.net

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.

Related

Taking a screenshot of only portion of the browser using selenium in VBA | "Selenium VBA"

so this one is about taking a screenshot of only a portion of the browser using Selenium in VBA. I saw there is something like
takescreenshot.hieght
takescreenshot.width
but not sure how to use these sub attributes height and width or if they will work.
Plus I have multiple images to save using different filenames automatically. I tried this but it did not work.
dim bot as chromedriver, img as selenium.image
bot.get "http:// xxxxxxx.....com"
..
..
for a = 1 to 100
set img = bot.takescreenshot(500)
img.Saveas this workbook.path & " :\" & a & ".jpg"
any suggestions please. Thanks in advance
There is unfortunately no built in function to take partial screenshots. However, you might try the following function to get an image of a specific element--please forgive any vb related mistakes I may have made as I rarely use the language.
Public Function CaptureElementScreenShot(ByVal element As HTMLElement, ByVal uniqueName As String) As Image
Dim screenshot As Screenshot = (CType(Me.driver, ITakesScreenshot)).GetScreenshot()
screenshot.SaveAsFile(filename, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim img As Image = Bitmap.FromFile(uniqueName)
Dim rect As Rectangle = New Rectangle()
If element IsNot Nothing Then
Dim width As Integer = element.Size.Width
Dim height As Integer = element.Size.Height
Dim p As Point = element.Location
rect = New Rectangle(p.X, p.Y, width, height)
End If
Dim bmpImage As Bitmap = New Bitmap(img)
Dim croppedImage = bmpImage.Clone(rect, bmpImage.PixelFormat)
Return croppedImage
End Function

Embedded base64String for images is to long for large images?

I am embedding a base64String into Tinymce to display images:
<img src="blob:https://path/871bf236-3bae-472c-9f02-0bd3be19a435" alt="Desert.jpg" width="300" height="75" />
It works for small images but when it comes to large images it doesn't work and I am guessing its because the URL containing the base64String for large images hits the limit for URL browser length which I believe is around 2000 characters. Wanted to see if there was a way to shorten my base64String?
'File path of the attachment
DIM filePath = C:\path\solutions\Attachments\1\1726014c-7a2d-41b8-a79e-2acec1e8c7e0
'Converted base64String path
DIM base64URLPath = ToBase64String( ConvertToUrl(filePath)).toString
'Converts the path to a base64String
Public Function ToBase64String(filePath As String) As String
Dim aImage = New Bitmap(filePath)
Using stream = New IO.MemoryStream
Using img As Image = Image.FromFile(filePath)
If img.RawFormat.Equals(Imaging.ImageFormat.Jpeg) Then
aImage.Save(stream, Imaging.ImageFormat.Jpeg)
ElseIf img.RawFormat.Equals(Imaging.ImageFormat.Png) Then
aImage.Save(stream, Imaging.ImageFormat.Png)
ElseIf img.RawFormat.Equals(Imaging.ImageFormat.Icon) Then
aImage.Save(stream, Imaging.ImageFormat.Icon)
End If
End Using
Return Convert.ToBase64String(stream.ToArray)
End Using
End Function
'Gets the full file path
Public Function ConvertToUrl(filePath As String) As String
Dim uri = New Uri(filePath).LocalPath
Dim converted = uri
Return converted.ToString()
End Function
Based on this post I fixed the issue: Resize and Compress image to byte array without saving the new image. The ToBase64String was the only function changed and now it looks like:
'Converts the path to a base64String
Public Function ToBase64String(filePath As String) As String
Dim aImage = New Bitmap(filePath)
Dim aspectRatio As Double = aImage.Height / aImage.Width
Dim imgThumb = New Bitmap(aImage, 200, CInt(Math.Round(200 * aspectRatio)))
Using stream = New IO.MemoryStream
Using img As Image = Image.FromFile(filePath)
If img.RawFormat.Equals(Imaging.ImageFormat.Jpeg) Then
imgThumb.Save(stream, Imaging.ImageFormat.Jpeg)
ElseIf img.RawFormat.Equals(Imaging.ImageFormat.Png) Then
imgThumb.Save(stream, Imaging.ImageFormat.Png)
ElseIf img.RawFormat.Equals(Imaging.ImageFormat.Icon) Then
imgThumb.Save(stream, Imaging.ImageFormat.Icon)
End If
End Using
Return Convert.ToBase64String(stream.ToArray)
End Using
End Function

Extract Images with text from PDF and Edit it using iTextSharp

I am trying to do following things in Windows Forms
1) Read a PDF in Windows Forms
2) Get the Images with Text in it
3) Color / fill the Image
4) save everything to a new file
I have tried Problem with PdfTextExtractor in itext!
But It didn't help.
Here is the code I've tried:
Public Shared Sub ExtractImagesFromPDF(sourcePdf As String, outputPath As String)
'NOTE: This will only get the first image it finds per page.'
Dim pdf As New PdfReader(sourcePdf)
Dim raf As RandomAccessFileOrArray = New iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf)
Try
For pageNumber As Integer = 1 To pdf.NumberOfPages
Dim pg As PdfDictionary = pdf.GetPageN(pageNumber)
' recursively search pages, forms and groups for images.'
Dim obj As PdfObject = FindImageInPDFDictionary(pg)
If obj IsNot Nothing Then
Dim XrefIndex As Integer = Convert.ToInt32(DirectCast(obj, PRIndirectReference).Number.ToString(System.Globalization.CultureInfo.InvariantCulture))
Dim pdfObj As PdfObject = pdf.GetPdfObject(XrefIndex)
Dim pdfStrem As PdfStream = DirectCast(pdfObj, PdfStream)
Dim bytes As Byte() = PdfReader.GetStreamBytesRaw(DirectCast(pdfStrem, PRStream))
If (bytes IsNot Nothing) Then
Using memStream As New System.IO.MemoryStream(bytes)
memStream.Position = 0
Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(memStream)
' must save the file while stream is open.'
If Not Directory.Exists(outputPath) Then
Directory.CreateDirectory(outputPath)
End If
Dim path__1 As String = Path.Combine(outputPath, [String].Format("{0}.jpg", pageNumber))
Dim parms As New System.Drawing.Imaging.EncoderParameters(1)
parms.Param(0) = New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 0)
'Dim jpegEncoder As System.Drawing.Imaging.ImageCodecInfo = iTextSharp.text.Utilities.GetImageEncoder("JPEG")'
img.Save(path__1) 'jpegEncoder, parms'
End Using
End If
End If
Next
Catch
Throw
Finally
pdf.Close()
raf.Close()
End Try
End Sub
Now, the actual purpose of this is to get something like this
If this is the actual PDF, I will have to check if there any any items in that bin(by Text in that box)
If there are items then I have to color it like below
Can someone help me with this
The PDF can be retrieved here.

Low quality thumbnails in listview VB.net

I use VB.net
I use ListView to view 50*x thumbnails(.jpg) at the left to the items.
But instead of 32bit high quality thumbnails, ListView lowers them to 8bit or 16bit(not sure).
Here's the code
Private Sub afterprocessed()
ListView1.Items.Clear()
Dim imlTemp As New ImageList
Dim dirFiles() As String = IO.Directory.GetFiles("backend\communicate\thumbnails")
Dim _imgList As New ImageList
Dim imgSize As New Size
imgSize.Width = 50
ListView1.SmallImageList = _imgList
Dim count As Integer = 0
Dim item As New ListViewItem
For Each dirFile As String In dirFiles
Dim imgFilename As String = IO.Path.GetFileNameWithoutExtension(dirFile)
Dim img As New System.Drawing.Bitmap(dirFile)
Dim imgImage As Image = Image.FromFile(dirFile)
'Dim imgHeight As Integer
'imgHeight = imgImage.Height
imgSize.Height = imgImage.Height
_imgList.ImageSize = imgSize
_imgList.Images.Add(img.Clone)
ListView1.Items.Add(imgFilename, count)
count += 1
Next
End Sub
And the quality is noticably low. Compared to original JPG
Any advice? I'd greatly appreciate it :D
You need this.
this.ListView1.SmallImageList.ColorDepth = ColorDepth.Depth32Bit;
I had a similar problem one time and I found out it was being caused by using the ImageList.ImageSize method, which seems to use a poor resizing algorithm. I resolved it by ensuring all the images I used were already the correct size on disk before loading them. But if that's not an option for you it shouldn't be too difficult to resize them before adding them to the ImageList.

How to save picture in the database

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