locked
Display Photo for Wm6.1 or higher RRS feed

  • Question

  • Hi

    I need some advice as to which method is faster and use less memory on displaying photo :

    The photo size is on the larger size   say ie : 600x800, 800x 400



    Method 1:

    1. get the photo file

    pictureBox1.Image = new Bitmap ( photo path);
    pictureBox1.SizeMode =

    PictureBoxSizeMode.StretchImage;

    Method 2:

    1. redraw the the large size photo into smaller size like 240x320 , 320x240

    2. PictureBox1.image = ReSizeImage;
    3. PictureBox1.sizeMode = PictureBoxSizeMode.StretchImage;


    Thanks



    Peace is the fruit of Love
    Tuesday, October 6, 2009 4:18 AM

Answers

  • Normally, I would suggest resizing it before you display it. But 600x800 and 800x400 is not really that large.

    If you decide to resize it anyway then here's a helper method for it:

    Image Stretch(Image image, Size size)
    {
      Image bmp = new Bitmap(size.Width, size.Height);
      using (var g = Graphics.FromImage(bmp))
      {
        g.DrawImage(
          image,
          new Rectangle(0, 0, size.Width, size.Height),
          new Rectangle(0, 0, image.Width, image.Height),
          GraphicsUnit.Pixel);
        }
      return bmp;
    }

    Tuesday, October 6, 2009 5:25 AM

All replies

  • Normally, I would suggest resizing it before you display it. But 600x800 and 800x400 is not really that large.

    If you decide to resize it anyway then here's a helper method for it:

    Image Stretch(Image image, Size size)
    {
      Image bmp = new Bitmap(size.Width, size.Height);
      using (var g = Graphics.FromImage(bmp))
      {
        g.DrawImage(
          image,
          new Rectangle(0, 0, size.Width, size.Height),
          new Rectangle(0, 0, image.Width, image.Height),
          GraphicsUnit.Pixel);
        }
      return bmp;
    }

    Tuesday, October 6, 2009 5:25 AM
  • Hi,

    10Q very much. It seems displaying without resizing with PictureBox takes a while.

    Does this do the Garbage collection --- using (var g = ....)?

      using (var g = Graphics.FromImage(bmp))
      {
        g.DrawImage(
          image,
          new Rectangle(0, 0, size.Width, size.Height),
          new Rectangle(0, 0, image.Width, image.Height),
          GraphicsUnit.Pixel);
        }


    One more question:

    If I have 10 to 20 images to be redrawn, How do I store them and display one by one ?It seems ImageList wont hold large images.

    How to create Bitmap dynamically for each image being created?



    Thanks


    Peace is the fruit of Love
    Tuesday, October 6, 2009 5:39 AM
  • The "using" statement will call Dispose() on IDisposable objects. The "var" keyword is just a C# 3.0 feature so that I don't have to state the obvious and do Graphics g = Graphics.FromImage().

    Do you want to display 20 images on the screen? I would suggest not to... But if you want to store a list of images you can use any other collection like a List or any key/value pair collection. Careful with caching large images this will hog up a lot of memory...

    Tuesday, October 6, 2009 6:07 AM