question

Theodosis-7186 avatar image
0 Votes"
Theodosis-7186 asked Theodosis-7186 edited

Is it possible to resize images and asynchronously load them in pictureboxes without saving?(Visual Basic)(Solved)

The pictures i need to load are pretty big in mb size. I am trying to resize their dimensions in order to reduce the ram usage when they are loaded in pictureboxes(they dont need to be perfect in quality as its something like a preview. But the command PictureBox1.LoadAsync() needs an image location path. I want to resize the images and instatly load them to pictureboxes without saving them in a hard drive. Is that possible?

Any suggestions about it or any further questions\ways to reduce ram usage are appriciated..


(I solved the issue (from 5-6 ram usage to <2) by loading the thumbnails of the images)

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered

Hi @Theodosis-7186 ,
The following example is to load pictures from a URL, then reset the resolution, and finally display one of them in picture box.
Here's the code you can refer to.

     Private newBitmap As Bitmap
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
     End Sub
     Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
         Dim imgLst As List(Of Bitmap) = Await GetImagesFromURL("image url")
         newBitmap = New Bitmap(imgLst(0))
         ' Set bitmap resolution
         newBitmap.SetResolution(50, 50)
         PictureBox1.Image = newBitmap
     End Sub
    
     Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
         PictureBox1.Image = Nothing
         newBitmap.Dispose()
     End Sub
     Private Async Function GetImagesFromURL(url As String) As Task(Of List(Of Bitmap))
         Dim images = New List(Of Bitmap)()
    
         Using client = New HttpClient()
             Dim response = Await client.GetAsync(url)
    
             If response IsNot Nothing AndAlso response.StatusCode = HttpStatusCode.OK Then
    
                 Using stream = Await response.Content.ReadAsStreamAsync()
                     Dim memStream = New MemoryStream()
                     Await stream.CopyToAsync(memStream)
                     memStream.Position = 0
                     images.Add(New Bitmap(memStream))
                 End Using
             End If
         End Using
    
         Return images
     End Function

Hope it could be helpful.

Best Regards,
Xingyu Zhao


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered

In case of WPF and UWP applications, see the usage of DecodePixelWidth property: https://docs.microsoft.com/en-us/dotnet/desktop/wpf/graphics-multimedia/how-to-load-an-image-as-a-thumbnail.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.