0

我一直在努力获取图像的 ROI,我正在尝试获得一个方形框,该框将悬停在该区域周围,并且能够单击并获得标准偏差。我对 c 很熟悉,所以我使用了 c 到 VB 转换器,但我收到错误,即该语句在命名空间中无效。一切似乎都与 VB 代码兼容。我将不胜感激有关此事的任何建议。谢谢

Private Function DrawRoi(Image As Bitmap, rect As RectangleF) As oid
Dim roi As New Rectangle()

roi.X = CInt(CSng(Image.Width) * rect.X)
roi.Y = CInt(CSng(Image.Height) * rect.Y)
roi.Width = CInt(CSng(Image.Width) * rect.Width)
roi.Height = CInt(CSng(Image.Height) * rect.Height)

Dim timer As New Stopwatch()
timer.Start()
' graphics manipulation takes about 240ms on 1080p image
Using roiMaskImage As Bitmap = CreateRoiMaskImage(ImageWithRoi.Width, ImageWithRoi.Height, roi)
    Using g As Graphics = Graphics.FromImage(ImageWithRoi)
        g.DrawImage(Image, 0, 0)
        g.DrawImage(roiMaskImage, 0, 0)
        Dim borderPen As Pen = CreateRoiBorderPen(ImageWithRoi)
        g.DrawRectangle(borderPen, roi)
    End Using
End Using
Debug.WriteLine("roi graphics: {0}ms", timer.ElapsedMilliseconds)
Me.imagePictureBox.Image = ImageWithRoi
End Function

Private Function CreateRoiMaskImage(width As Integer, height As Integer, roi As Rectangle) As Bitmap
Dim image As New Bitmap(width, height, PixelFormat.Format32bppArgb)
Using g As Graphics = Graphics.FromImage(image)
    Dim dimBrush As New SolidBrush(Color.FromArgb(64, 0, 0, 0))
    g.FillRectangle(dimBrush, 0, 0, width, height)
    Dim roiBrush As New SolidBrush(Color.Red)
    g.FillRectangle(roiBrush, roi)
    image.MakeTransparent(Color.Red)
    Return image
End Using
End Function
4

0 回答 0