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

Theodosis 41 Reputation points
2021-06-04T17:53:22.253+00:00

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)

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,614 questions
0 comments No comments
{count} votes

Accepted answer
  1. Xingyu Zhao-MSFT 5,356 Reputation points
    2021-06-07T05:47:06.507+00:00

    Hi @Theodosis ,
    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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Viorel 113.7K Reputation points
    2021-06-04T19:21:17.923+00:00

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

    0 comments No comments