0

I'm writing a program that will allow the user to download selected images from Azure Blob Storage.

I have it working, however, currently, the images are downloaded to a file and then this file path is used to display the images. I want the images to be displayed and then allow the user to select which images can be downloaded.

Below is my code for downloading the images.

for (int i = 1; i<=dira.ListBlobs().Count(); i++)
{
     try
     {
          CloudBlob blob = dira.GetBlobReference(i + ".png");
          blob.DownloadToFile(localFilePath + "/" + i.ToString() + ".png", FileMode.Create);
          // MessageBox.Show(i.ToString());
      }
      catch (StorageException ex)
      {
      }
}

Then my code for displaying the downloaded image is here:

pictureBox1.BackgroundImage= Image.FromFile(filePath + ".png");

How would I display the images before they have been downloaded?

8
  • You may need to upload two version of your images, a full resolution version and a thumbnail version and then use the thumbnail version to display the images to your users before downloading the full one.
    – Isma
    Jul 2, 2018 at 9:32
  • Does the CloudBLob give you the url of the image?
    – Haytam
    Jul 2, 2018 at 9:33
  • 1
    You can't display image without downloading image (or some thumb of image)
    – vasily.sib
    Jul 2, 2018 at 9:33
  • @Haytam not from what I can see. is there a way without having to download them to a file though? So download them to memory. Display them and then if the images are wanted them download them to file?
    – benjiiiii
    Jul 2, 2018 at 9:40
  • 1
    Well if you had the url of the image then you could using PictureBox.Location which will download the image but in memory.
    – Haytam
    Jul 2, 2018 at 9:45

3 Answers 3

2

As you said above, we can download them to memory.

Here is simple code for your reference:

 CloudBlob blob = dira.GetBlobReference(i + ".png");
 MemoryStream memoryStream = new MemoryStream();
 blob.DownloadToStream(memoryStream);
 pictureBox1.BackgroundImage = System.Drawing.Image.FromStream(memoryStream);
1

If you want to really save some network traffic (aka time to download) between PC and Blob storage all you have to do is create thumbnail in Azure.

I found a very nice and complete example how to do that. Mechanism is pretty neat and 'cloudy'

Thumbnail in cloud

Please bear in mind, that above could raise your Azure bill. As in other cases in this too you need to consider what are your priorities:

  • I need to be super quick and save network for my users -> create thumbnails in Azure

  • I want to save costs on my side and performance is not a concern -> download full sized image and create thumbnail on host

0

You can not show images without downloading it

But instead,

You should create a thumbnail image with your actual image so when you show the list to a user you can download thumbnails from the server and then actual images on user selection

you can create thumbnail using below code

    public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height) 
    { 
        //a holder for the result 
        Bitmap result = new Bitmap(width, height); 

        //use a graphics object to draw the resized image into the bitmap 
        using (Graphics graphics = Graphics.FromImage(result)) 
        { 
            //set the resize quality modes to high quality 
            graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
            //draw the image into the target bitmap 
            graphics.DrawImage(image, 0, 0, result.Width, result.Height); 
        } 

        //return the resulting bitmap 
        return result; 
    } 

Ref:- C# Creating thumbnail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.