Set lists of images using bitmap class on Windows Phone - vb.net

i recently start developing windows phone apps ... well i tended to try using an image control to display a picture on it by setting the code below :
Dim myimages As New BitmapImage(New Uri("/Add Radcontrols test;component/Images/screen.PNG", UriKind.Relative))
Image1.Source = myimages
til now everything is perfectly working but .. i wondered if i could add more than one image and navigate between them by hitting a particular button existing in the UI of my windows phone emulator .

You can do it by creating a List of image uri string. And upon a button clicked, create new BitmapImage using next image uri in the List, then set image1's Source to that newly created BitmapImage. That will be less memory-consuming then if you load all images at once and navigate between them. If those images are stored locally (not downloaded from internet) as what you seems to be doing currently, you'll see the effect as if all images loaded at once. No noticeable delay when loading next image, that what I see so far in emulator.
Dim imageUris As New List(Of String)
Dim nextImageIndex As Integer
...
...
'Upon button click
Dim myImage As New BitmapImage(New Uri(imageUris(nextImageIndex), UriKind.Relative))
Image1.Source = myimage
nextImageIndex += 1
'if index > last uri index, reset index
If nextImageIndex = imageUris.Count Then nextImageIndex = 0 End

Related

How to make invisible text visible with iText 4 in an existing pdf

I have a PDF which is created by scanning software. One image per page and hidden OCR'ed text.
I want to remove the images and make the text visible.
I found info how to remove images (replace by another image) but found no way for making the invisible text visible.
Sample PDF with image and hidden text
I tried below method, but it does not work:
Public Shared Sub UnhideText(ByVal strFileName As String)
Dim pdf As iTextSharp.text.pdf.PdfReader = New iTextSharp.text.pdf.PdfReader(strFileName)
Dim stp As iTextSharp.text.pdf.PdfStamper = New iTextSharp.text.pdf.PdfStamper(pdf, New IO.FileStream("e:\out.pdf", IO.FileMode.Create))
'This does not work, text remains unvisible. I guess SetTextRenderingMode applies only for new added text.
For pageNumber As Integer = 1 To pdf.NumberOfPages
Dim cb As iTextSharp.text.pdf.PdfContentByte = stp.GetOverContent(pageNumber)
cb.SetTextRenderingMode(iTextSharp.text.pdf.PdfContentByte.TEXT_RENDER_MODE_FILL)
Next
stp.Close()
End Sub

Resizing multiple images and saving them to a separate folder

I need to resize and compress 200 images that I have stored in a folder.
I am getting these images in a list using this code that I got from another question:
Dim dir = New IO.DirectoryInfo("C:\\Users\\Charbel\\Desktop\\Images")
Dim images = dir.GetFiles("*.jpg", IO.SearchOption.AllDirectories).ToList
Dim pictures As New List(Of PictureBox)
For Each img In images
Dim picture As New PictureBox
picture.Image = Image.FromFile(img.FullName)
pictures.Add(picture)
Next
Now, I need to compress and reduce each image to (500x374) and then save them in another folder on my PC.
Well, let me first point out a couple of points about your code:
PictureBox doesn't serve any purpose here. You shouldn't create a PictureBox to use the Image.
Always remember to dispose the Image object (e.g., by wrapping it in a Using block) so you don't run into memory issues.
Unlike C#, VB.NET doesn't require escaping the \ character, therefore, you can write your path like this "C:\Users...".
Now, for resizing the image, you can simply create an instance of the Bitmap class with the constructor that takes an image and a size argument: Bitmap(Image, Size) or Bitmap(Image, Int32, Int32).
Here:
Dim sourcePath As String = "C:\Users\Charbel\Desktop\Images"
Dim outputPath As String = "C:\Users\Charbel\Desktop\Images\Resized"
IO.Directory.CreateDirectory(outputPath)
Dim dir = New IO.DirectoryInfo(sourcePath)
Dim files As IO.FileInfo() = dir.GetFiles("*.jpg", IO.SearchOption.AllDirectories)
For Each fInfo In files
Using img As Bitmap = Image.FromFile(fInfo.FullName)
Using resizedImg As New Bitmap(img, 500, 374)
resizedImg.Save(IO.Path.Combine(outputPath, fInfo.Name),
Imaging.ImageFormat.Jpeg)
End Using
End Using
Next

How to save data to text file and retrieve

I'm using VB.NET. I am able to load the pics from a folder into a flowlayoutpanel. And then load the clicked picture into a separate picturebox and display the picture's filepath in a label.
Now I want to be able to add rating and description to each of the image in the flowlayoutpanel and save it to a text file in the folder from which the pictures have been loaded. The app should load be able to load the rating and description on the next launch or when the selected image is changed. How do I accomplish this?
You should probably look at accessing the metadata of the pic. This way the info you want is carried with the pic. This is contained in the PropertyItems Class, which is a property of the Image class
Here's a link to an answered question about adding a comment to a jpg. Hope this helps.
Here's an untested conversion of that code in VB.net. You'll probably have to add a reference or 2 and import a couple of namespaces, but syntactically this is correct as near as I can tell.
Public Function SetImageComment(input As Image, comment As String) As Image
Using memStream As New IO.MemoryStream()
input.Save(memStream, Imaging.ImageFormat.Jpeg)
memStream.Position = 0
Dim decoder As New JpegBitmapDecoder(memStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad)
Dim metadata As BitmapMetadata
If decoder.Metadata Is Nothing Then
metadata = New BitmapMetadata("jpg")
Else
metadata = decoder.Metadata
End If
metadata.Comment = comment
Dim bitmapFrame = decoder.Frames(0)
Dim encoder As BitmapEncoder = New JpegBitmapEncoder()
encoder.Frames.Add(bitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metadata, bitmapFrame.ColorContexts))
Dim imageStream As New IO.MemoryStream
encoder.Save(imageStream)
imageStream.Position = 0
input.Dispose()
input = Nothing
Return Image.FromStream(imageStream)
End Using
End Function

How can I show a picture on a form in VB.NET?

I am creating questionnaires using VB.NET and SQL and my problem now is how can I show an image already present in the database onto a form? Please note that as the form is a questionnaire, it is navigable by the means of going up or down rows in a dataset. For example when the NEXT button is clicked, the row in the dataset goes +1. So how should I go about coding it in order to display an image onto the form?
this is the code how i saved the image
Dim ms As New MemoryStream()
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
Dim data As Byte() = ms.GetBuffer()
Dim x As New SqlParameter("#image", SqlDbType.Image)
x.Value = data
cmd.Parameters.Add(x)
And the code for navigating between rows of a dataset in a single form:
RichTextBox1.Text = dsquestionnaire.Tables(0).Rows(qsno).Item("Question") .....
qsno + 1 (As a part of the NEXT button click event)
Thanks in advance..
You can load the image from the DB into your picture box as follows:
Using ms As New IO.MemoryStream(CType(row("image", Byte()))
Dim img As Image = Image.FromStream(ms)
Image1.Image = img
For the click event you can place this inside a method and just call it to load the image into the Picture box when the next button is clicked

Copying pictures from inside an iFrame in webbrowser control

I used the code below to successfully get a copy of each picture inside a page loaded using webbrowser control.
Dim doc As IHTMLDocument2 = DirectCast(wb.Document.DomDocument, IHTMLDocument2)
Dim imgRange As IHTMLControlRange = DirectCast(DirectCast(doc.body, HTMLBody).createControlRange(), IHTMLControlRange)
For Each img As IHTMLImgElement In doc.images
imgRange.add(DirectCast(img, IHTMLControlElement))
imgRange.execCommand("Copy", False, Nothing)
Using bmp As Bitmap = DirectCast( _
Clipboard.GetDataObject().GetData(DataFormats.Bitmap), Bitmap)
bmp.Save(img.nameProp)
End Using
Next
I got the code from here: Copy an image from cache of web browser control present in VB.NET
However, the picture I am interested in is inside an iFrame.
I tried changing:
Dim doc As IHTMLDocument2 = DirectCast(wb.Document.DomDocument, IHTMLDocument2)
to
Dim doc As IHTMLDocument2 = DirectCast(wb.Document.Window.Frames(iFrameID).Document.DomDocument, IHTMLDocument2)
but I am getting an "Access Denied" Error. I guess (not sure) its because the iframe's src is on a different domain.
Is there a way around this problem?
Thanks!
If the iFrame has content from a domain that is different from the the parent then your out of luck. You could attempt a different solution and retrieve the iFrame's page via Ajax and parse out the image src. Another alternative is to do it server side with a program like PhantomJs (or VB, PHP etc) to retrieve the page and parse it for images to retrieve.