PictureBox 运行时图像
要查看问题,请按照以下步骤操作(我正在使用 C# 进行编码):
- 将图片框放在名为 picturebox1 的窗体上
- 将其大小调整为小矩形(例如 75,75)
- 将图片添加到比该矩形大的资源(名为 pic) (比如说 500,500)
现在我们准备好了 :D
在图片框中显示这张图片,同时显示整个图片而不仅仅是它的一部分:
- 添加图像列表和设置大小(这一点都不好)
- 我知道有两种方法可以在运行时 该图片框的 onPaint 是这样的:
e.Graphics.DrawImage(Properties.Resources.pic, 新矩形(0,0,75,75));
假设我想填充 picturebox1 的 Image 属性,现在我必须编写如下内容:
pictureBox1.Image = (Image)Properties.Resources.ResourceManager.GetObject("pic1");
它不会调整检索到的图像的大小。我想知道是否有解决这个问题的好方法(轻松调整大小和分配)?顺便说一句,我不想使用类似的东西调整图片大小
现在第二个问题:
我知道资源是静态添加在我的例如,但是有没有办法以编程方式获取资源的名称,而不是将其硬编码为字符串并将其传递给“GetObject()”方法?
To see the problems, please follow these steps (I am coding in C#):
- Put a picturebox on a form named picturebox1
- Resize it to small rectangle (say 75,75)
- 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:
- Adding Imagelist and setting size (this is not good at all)
- 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
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,PictureBox 不会调整图像大小以适合其显示区域。试试这个:
By default PictureBox doesn't size the image to fit with its display area. Try this: