【发布时间】:2012-08-14 07:18:41
【问题描述】:
我正在尝试从 .NET 中的 PictureBox 复制图像并将其绘制在另一个图像之上。当我在初始图像上绘制图像时,如何保留图片框中图像的 sizemode 属性?
提前谢谢...
edit:这是我用来将 PictureBox (pbImage) 中的图像绘制到另一个 picturebox (pbContainer) 中的图像的代码。我将 SizeMode 设置为缩放 pbImage。我遇到的问题是尝试使用与 pbImage 中相同的外观来绘制图像,并且想知道是否有办法找出图像的大小,因为它使用 SizeMode = Zoom 而不是如何显示在图片框中原始图像已调整大小。
Private Function FlattenImage() As Bitmap
Dim bmp As New Bitmap(pbContainer.Image, pbContainer.Size)
Using insertImage As New Bitmap(pbImage.Image, pbImage.Width, pbImage.Height), g As Graphics = Graphics.FromImage(bmp)
Dim insertLocation As New Point(pbImage.Left - pbContainer.Left, pbImage.Top - pbContainer.Top)
g.DrawImage(insertImage, insertLocation)
Return bmp
End Using
End Function
我最终只是像这样使用 DrawToBitmap():
Private Function FlattenImage() As Bitmap
Dim bmp As New Bitmap(pbContainer.Image, pbContainer.Size)
Using insertImage As New Bitmap(pbImage.Width, pbImage.Height), g As Graphics = Graphics.FromImage(bmp)
Dim insertLocation As New Point(pbImage.Left - pbContainer.Left, pbImage.Top - pbContainer.Top)
Dim rect As New Rectangle(New Point(0, 0), pbImage.Size)
pbImage.DrawToBitmap(insertImage, rect)
g.DrawImage(insertImage, insertLocation)
Return bmp
End Using
End Function
它可以按我的需要工作。但是我仍然有兴趣了解是否有一种方法可以使用各种 SizeModes 来获取图像在 PictureBox 中显示时的大小,因为它在未来可能会有用。
谢谢。
【问题讨论】:
-
为什么它同时标记为 C# 和 VB.NET?这使您看起来甚至没有编写任何代码来尝试完成此操作...
-
@codesparkle - 很多人都乐于收到任何一种语言的答案,但我同意以一些代码为起点会很好
-
@codesparkle - 我已经用我用来更好地解释问题的代码更新了帖子。
-
@MattWilko - 这就是原因。两种语言都适合我。我添加了我用来更好地解释问题的代码。