我正在尝试从.NET中的PictureBox复制图像并将其绘制在另一个图像之上。当我在初始图像上绘制图像时,如何保留图片框中图像的sizemode属性?
提前致谢...
编辑:这是我用来将PictureBox(pbImage)中的图像绘制到另一个图片框(pbContainer)的图像上的代码。我将SizeMode设置为放大pbImage。我遇到的问题是尝试使用与pbImage中相同的外观来绘制图像,并且想知道是否有办法找出图像的大小,因为它在Picturebox中以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中显示,因为它可能在将来有用。
感谢。
答案 0 :(得分:0)
例如,将PictureBox1.BackColor设置为Transparent,而不是:
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