0

To see the problems, please follow these steps (I am coding in C#):

  1. Put a picturebox on a form named picturebox1
  2. Resize it to small rectangle (say 75,75)
  3. Add a picture to resources (named say pic) larger than that rectangle (say 500,500)

Now we are ready :D

I know two ways to have this picture shown in the picturebox at runtime while showing the whole picture not only a part of it:

  1. Adding Imagelist and setting size (this is not good at all)
  2. Writing onPaint of that picturebox something like this:

e.Graphics.DrawImage(Properties.Resources.pic, new Rectangle(0,0,75,75));

Lets assume that I want to fill the Image property of the picturebox1, now I must write something like this:

pictureBox1.Image = (Image)Properties.Resources.ResourceManager.GetObject("pic1");

which does not resize the image retrieved. I want to know if there is a good way around this problem (to resize and assign easily)? BTW, I don't want to resize the picture using something like these

  1. Image Editing Using C#
  2. Image Editing while maintaining aspect ratio

Now the second question:

I know resources are statically added in my example, but is there a way to get the name of the resource programmatically instead of hardcoding it as string and passing it to the "GetObject()" method?

leppie
  • 115,091
  • 17
  • 196
  • 297
Yasser Sobhdel
  • 611
  • 8
  • 26
  • I forgot to tell that there is a way to assign image (but still static) by Properties.Resources.pic1. I don't want it cause it is static and does not give me the name of the resource. – Yasser Sobhdel Mar 01 '11 at 09:47

1 Answers1

1

By default PictureBox doesn't size the image to fit with its display area. Try this:

pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
CharlesB
  • 86,532
  • 28
  • 194
  • 218