Loading images from a folder [VB.NET] - vb.net

I'm currently experiencing a problem with my program. I want to create a photo gallery for my girlfriend which she can install on her computer. I want to import images from a folder on Form load and display them in a PictureBox.
When I load the Form I get a big red 'X' that fills the box. Looks like the drawing.bitmap ErrorImage. What could be the problem? Any help is greatly appreciated. Thank you.
Private Sub Pigge_Gallary_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim directory As New IO.DirectoryInfo("C:\Pics\Bears")
If directory.Exists Then
Dim jpgFiles() As IO.FileInfo = directory.GetFiles("*.jpg")
For Each jpgFile As IO.FileInfo In jpgFiles
If jpgFile.Exists Then
Dim image = Drawing.Image.FromFile(jpgFile.FullName)
Using image
PicPig.Image = image
End Using
End If
Next
End If
End Sub

Once you exit the Using block, you're losing the image reference.
Try changing
PicPig.Image = image
to
PicPig.Image = image.clone
or just set it to the image from the file:
PicPig.Image = Drawing.Image.FromFile(jpgFile.FullName)
Also, because you're doing it in a For loop, it's just going to replace the picturebox image as it loops. When it finishes the loop, the picturebox will only be displaying the last image.

Related

XtraReport show image in picturebox one time only

I've created an xtrareport that contains picturebox and i set the image through xrPictureBox1.ImageUrl in XrPictureBox1_BeforePrint event.
when i open the report for the first time, the images showing properly, but when reload report and reopen it again the images not showing.
I'm using version 17.2.
please help!
Regards.
Private Sub XrPictureBox1_BeforePrint(sender As Object, e As PrintEventArgs) Handles XrPictureBox1.BeforePrint
XrPictureBox1.ImageUrl = HttpContext.Current.Server.MapPath("~/TaskFiles/" & XrTableCell9.Text)
End Sub

How change image in generated imagebox vb

I have to do a program where I can generate Button, Label, ImageBox and work with them. Now I am searching how change image in created ImageBox, it writes: this element doesn't exist. How can I get access to the created elements?
Dim PictureB As New PictureBox
PictureB.Size = New System.Drawing.Size(200, 120)
PictureB.Location = New System.Drawing.Point(350, 20)
PictureB.BorderStyle = BorderStyle.Fixed3D
TabPage1.Controls.Add(PictureB)
"New sub"
OpenFileDialog1.ShowDialog()
PictureB.ImageLocation = OpenFileDialog1.FileName
PictureB is not accessible in your "new sub" because it only exists in the subprocedure that you created it in as a temporary, private variable. The PictureBox, however, that you created, does exist in TabPage1.Controls, so you can access it in a loop.
Based on your requirements, it seems that you also need to get the exact PictureBox that you created, so I would suggest adding a Name to it when you create it, something like PictureB.Name = "Pic1". I have included a sample Sub below that shows how you might accomplish your goal.
Public Sub SetImage(ByVal PictureBoxName As String)
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
For Each c As Control In TabPage1.Controls
Dim pb As PictureBox = TryCast(c, PictureBox)
If Not IsNothing(pb) Then
If pb.Name = PictureBoxName Then
pb.ImageLocation = OpenFileDialog1.FileName
Exit For
End If
End If
Next
End If
End Sub
Alternatively, you could look into creating event handles instead: that would be more involved, but would be a good learning experience.

VB.Net 2012 Copy the file when a picture is dragged

I have a picture icon on my form. I would like to have it so when the user drags this icon to a windows explorer, desktop etc the associated file is written to that location.
I have the file path in a combo and as I said an image on the form for the user to drag. The image is just a general image not really associated with the actual file.
Can you help me out? I have no clue!
For the next guy....
Where pbDragger is the picture box.
It is important to note that the DataObject requires a string array even if you are only dragging one file.
Private Sub pbDragger_MouseMove(sender As Object, e As MouseEventArgs) Handles pbDragger.MouseMove
If ((e.Button And MouseButtons.Left) = MouseButtons.Left) Then
Dim strPath As String = cboRef.SelectedItem!Path.ToString ' Path of the file to be copied
Dim strArr() As String = {strPath} ' 'FileDrop requires an array of string!!
Dim oDraginfo As New DataObject(DataFormats.FileDrop, strArr)
Dim dropEffect As DragDropEffects = pbDragger.DoDragDrop(oDraginfo, DragDropEffects.Copy)
End If
End Sub

Picturebox change if button is clicked

Hi good day guys i would just like to ask if how can I change a picture in a PictureBox if I clicked a button to be specific i have one picture box and one button which is Button1 and I want to change the picture everytime I click the Button1. Thanks in advance :)
In your button1_click event, you could add the following
PictureBox1.Image= Image.FromFile("c:\folder\file.gif")
Replace the path with the image you need.
EDIT
To change it everytime, you need to create a global variable 'counter'.
Add to that value everytime you click.
Also, create an array of pictures.
So everytime you click, you select the string in the array with the index of counter and you set that as the image in the code presented above.
Dim array() As String = {"c:\folder\file1.gif", "c:\folder\file2.gif", "c:\folder\file3.gif"}
PictureBox1.Image= Image.FromFile(array(counter))
You can create a List of Image contained into Somefilepath
Dim images As New List(Of Image)()
images.add(Image.FromFile(Somefilepath))
Dim imageindex as Integer
imageIndex = 0
Now in a separate function you can change your pictures every time the Button1 is clicked
Private Sub Button1_Click(sender As Object, e As EventArgs)
PictureBox1.Image = images(imageIndex)
imageIndex = imageIndex + 1
End Sub

VB.NET Tool for Image Printing - Quality Ajustments

I'm developing a tool for easy picture printing with a canon selpy cp800. The Image is printed with the following methods:
Private Sub BtnPrintClick(sender As Object, e As System.EventArgs) Handles ptnPrint.Click
If PrintDialog1.ShowDialog() = DialogResult.OK Then
pdPrintImage.Print()
End If
End Sub
Private Sub PdPrintImagePrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles pdPrintImage.PrintPage
e.Graphics.DrawImage(_dPictures(_sPictures(_iActiveImage)).Picture, e.Graphics.VisibleClipBounds)
End Sub
_dPictures(_sPictures(_iActiveImage)).Picture --> object of the type image
I didn't do anything with this image. It's only loaded with the Image.FromFile() method.
Within the following image you can see my problem. This is a scan of the image printed with this method (top) and a scan of the same image printed with the windows picture viewer. You see, the first image you see the tonal errors in the background and the shadows.
Has anyone an idea to fix this?
If it isn't a bit-depth issue as Boo mentioned, it might help to set one or both of these
e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
before doing the .DrawImage.
the best quality I achieved as follows:
1) I put a picture into pdf using components iTextsharp.
2) print the pdf