I'm having trouble loading an image from My.Resources. I have already tried a no. of codes like....:
PictureBox1.Image = My.Resources.Online_lime_icon; And
PictureBox1.Image = CType(My.Resources.ResourceManager.GetObject("Online_lime_icon"), Image)
but it would still return:
Picturebox1.Image = Nothing
Try to convert it ToBitMap()
PictureBox1.Image = My.Resources.Online_lime_icon.ToBitmap()
EDIT:
#user1615710 : My.Resources.Online_lime_icon doesn't have .ToBitmap. It only has .ToString.
That means you've String resource and I think it represents fileName with or without absolute path.
Try to print/show the content of My.Resources.Online_lime_icon
Debug.Print(My.Resources.Online_lime_icon) 'or
MsgBox(My.Resources.Online_lime_icon)
If it is real path then load the image via,
PictureBox1.Image = Image.FromFile(My.Resources.Online_lime_icon)
Related
I want to know size of a image in a PictureBox, but cannot to access the Image property, because it's nothing.
Dim PiBox1 As New PictureBox
With PiBox1
.ImageLocation = "image.png"
.SizeMode = PictureBoxSizeMode.Zoom
.Dock = DockStyle.fill
If .Image Is Nothing Then MsgBox("Image = Nothing")
End With
PictureBox.Size, PictureBox.ClientSize return dimensions of the control, not the image inside...
While there are other options, to change the code as little as possible, just add a call to the Load method after setting the ImageLocation property. That will force the Image to be loaded synchronously.
That said, you really ought to be setting the Dock and SizeMode properties first. Why would you load an Image first and then change the size afterwards? Do all the configuration first, then load the Image as the last step. This is akin to setting the DataSource last when data-binding.
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
I'm using this code to save image
PictureBox1.Image.Save(filePath)
after that I crop image and I want to save it again by replacing the old one with the new cropped one
any help please
regards,,,
Use the following code when you load the image into the PictureBox instead of the one you are currently using and you'll be fine just saving later. The using statement ensures the file is released once the image is loaded.
Replace filePath with your own.
Using stream as new FileStream(filePath, FileMode.Open, FileAccess.Read)
PictureBox1.Image = Image.FromStream(stream)
End Using
EDIT
From your last comment I can see this code
Try
Me.Opacity = 0%
Me.PictureBox1.Image = cc()
PictureBox1.Image.Save(filePath)
source = Image.FromFile(filePath)
PictureBox1.Image = source
TextBox1.Text = filePath
Me.Opacity = 100%
Catch ex As Exception
MsgBox(ex.Message)
End Try
You don't need to reload the image to the PictureBox after saving. Just get rid of the following lines.
source = Image.FromFile(filePath)
PictureBox1.Image = source
That should solve your problem at least for now as you won't be saving the image to the same image you load it from (you didn't actually load it). But you will have to find a better solution for your whole algorithm later :)
This's the answer
Dim bmp1 As New Bitmap(PictureBox1.Image)
If System.IO.File.Exists(filePath) Then
System.IO.File.Delete(filePath)
End If
bmp1.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg)
' Dispose of the image files.
bmp1.Dispose()
I have a picturebox named PB_Company_Logo and I have a button named btn_Save and within this button I have this function which saves the image in PB_Company_Logo to current_directory/images
Public Sub save_PB(PB_Name As PictureBox)
Dim filename As String = "company_logo.png"
Dim path As String = Directory.GetCurrentDirectory() & "\images"
Dim filename_path As String = System.IO.Path.Combine(path, filename)
If (Not System.IO.Directory.Exists(path)) Then
System.IO.Directory.CreateDirectory(path)
PB_Name.Image.Save(filename_path)
Else
PB_Name.Image.Save(filename_path)
End If
End Sub
The problem is, there are cases where the user will upload a new company_logo.png. I want the system to treat the uploading of new image as replacing the former company_logo.png.
I think the error in this line of code means that the file is currently in used (locked) and therefore cannot be replaced.
Else
PB_Name.Image.Save(filename_path)
End If
When you load a pictureBox control with an image file the ide (vs) puts a lock on the file. This happens when you set the image property of the pictureBox control to a file at design time.
You can use the FileStream object.
Example:
Dim fs As System.IO.FileStream
' Specify a valid picture file path on your computer.
fs = New System.IO.FileStream("C:\WINNT\Web\Wallpaper\Fly Away.jpg",
IO.FileMode.Open, IO.FileAccess.Read)
PictureBox1.Image = System.Drawing.Image.FromStream(fs)
fs.Close()
After you have done the workaround you can then use the System.IO.File.Exists(img) namespace to check wether or not a picture exists.
Example: This checks if the image passed through already exists and if it does then it will replace it.
Dim imageStr As String = "~/images/name.jpg"
If System.IO.File.Exists(imageStr) Then
Image1.ImageUrl = "~/images/name.jpg"
End If
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