从父图片框图形(不是背景图像)创建图像

时间:2015-05-24 15:44:35

标签: vb.net

编辑:我有点成功,并认为这可能无效。目前这种情况很糟糕,Panel1在我的循环中被刷新(),但这不是问题,可以在以后的渲染方法中修复。取消自定义控件,并绕过不透明度的东西,我在主窗体上的基本面板绘制事件中使用以下内容:

Dim tempImage = New Bitmap(Picturebox1.Width, Picturebox1.Height)
Dim memoryGraphics As Graphics = Graphics.FromImage(tempImage)
Dim screenPos As Point = Picturebox1.PointToScreen(New Point(0, 0))
memoryGraphics.CopyFromScreen(screenPos.X, screenPos.Y, 0, 0, Picturebox1.Size)

Dim destRect As New Rectangle(0, 0, Panel1.Width, Panel1.Height)
Dim srcRect As New Rectangle(picscreen.Location.X, picscreen.Location.Y, Panel1.Width, Panel1.Height)
e.Graphics.DrawImage(tempImage, destRect, srcRect, GraphicsUnit.Pixel)

在我的表单上:Picturebox1.Location(208,0)& Panel1.Location(208,0)。两个控件父级必须是主窗体,我不能将Panel1的父级指定为Picturebox1。

我们的想法是在地图上放置一个半透明的面板(Picturebox1图形)。使用上面的代码,我显示了从绘制到Picturebox1的内容到Panel1的偏移量大约208像素(或两个控件的左侧位置)。

我可以稍微调整代码中的某些点以减少偏移量,但这将开始显示Picturebox1上Panel1的灰色背景:[即Dim srcRect As New Rectangle(picscreen.Location.X - 100。 ..]将从面板1给我100像素的灰色,并显示100个像素更接近排列...更多使得全灰色Panel1背景颜色出现:

Temp

因为它从Picturebox1上的所有内容中获取图像,所以它也获得了Panel1,而不是它下面的内容。我不相信我想做的事情是可能的

1 个答案:

答案 0 :(得分:0)

您可以获取picbox和面板的设备上下文,并将您想要的面板部分设为bitbox:

<DllImport("GDI32", SetLastError:=True)> Private Shared Function BitBlt( _
    ByVal hdcDest As IntPtr, _
    ByVal nXDest As Integer, _
    ByVal nYDest As Integer, _
    ByVal nWidth As Integer, _
    ByVal nHeight As Integer, _
    ByVal hdcSrc As IntPtr, _
    ByVal nXSrc As Integer, _
    ByVal nYSrc As Integer, _
    ByVal dwRop As Integer) As Boolean
End Function

Const SRCCOPY As Integer = &HCC0020

Dim picboxGraphics As Graphics = PictureBox1.CreateGraphics
Dim pnlGraphics As Graphics = Panel1.CreateGraphics
Dim PictureBox_HDC As IntPtr = picboxGraphics.GetHdc
Dim Panel_HDC As IntPtr = pnlGraphics.GetHdc
BitBlt(Panel_HDC, 0, 0, 200, 200, PictureBox_HDC, 0, 0, SRCCOPY) '0, 200 are arbitrary

picboxGraphics.ReleaseHdc(PictureBox_HDC)
pnlGraphics.ReleaseHdc(Panel_HDC)
picboxGraphics.Dispose()
pnlGraphics.Dispose()