【问题标题】:Drawing images in .NET from another picturebox and keeping sizemode从另一个图片框在 .NET 中绘制图像并保持 sizemode
【发布时间】: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 - 这就是原因。两种语言都适合我。我添加了我用来更好地解释问题的代码。

标签: .net vb.net


【解决方案1】:

例如,将 PictureBox1.BackColor 设置为透明,而不是:

Dim x As New Bitmap(PictureBox1.Width, PictureBox1.Height)
PictureBox1.DrawToBitmap(x, PictureBox1.DisplayRectangle)

之后,通过使用循环,您可以从左、右、上和下计算区域。例如:

If Not (x.GetPixel(i, j) = Color.Transparent) Then
EdgeFound(i,j)
End If

【讨论】:

    猜你喜欢
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 2012-06-15
    • 1970-01-01
    相关资源
    最近更新 更多