Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

VS 2010 Problem with Memory management.

$
0
0
Hi All,
In my project I'm trying to add feature to increase/decrease the brightness and contrast of the displayed image. I'm doing this based on user's mouse movement over the image in horizontal and vertical direction. Every time I'm adjusting the contrast and brightness my process memory in task manager is getting increased and not getting released. The code is as below:
Code:


Public ImageDataBuffer_InProcess(RC_Buffer_Size) As Byte 'Buffer holding image data after performing opted image processing operations.

 Public Function ImageScaling_16BitTo8Bit(ByVal ImageData() As Byte, ByVal WindowWidth As Integer, ByVal WindowCenter As Integer) As Byte()

        Dim ij As Integer = 0
        Dim jj As Integer = 0
        Dim kk As Integer = 0
        Dim min As Integer = Convert.ToInt32(WindowCenter - (WindowWidth / 2))
        Dim max As Integer = Convert.ToInt32(WindowCenter + (WindowWidth / 2))
        Dim scale, scale2 As Double
        Dim PixelValue(Image_Resolution_Size) As Integer
        Dim Scaled_8Bit_ImageData((Rows * Columns) - 1) As Byte

        'pixel_Average = 0

        For i As Integer = 0 To Rows - 1

            For j As Integer = 0 To Columns - 1
                PixelValue(jj) = CInt(ImageData(ij)) + CInt(ImageData(ij + 1) * 256)

                'pixel_Average += scaledata(jj)

                ij = ij + 2
                jj = jj + 1

            Next

        Next

        ''pixel_Average = pixel_Average / (Rows * Columns)
        'MessageBox.Show(pixel_Average)

        kk = 0

        Dim iscale, maxmin As Integer
        maxmin = max - min

        'Be on safer side
        If (maxmin <= 0) Then
            maxmin = 1
        End If

        scale = 255 / maxmin

        For a As Integer = 0 To Rows - 1

            For b As Integer = 0 To Columns - 1

                If (PixelValue(kk) < min) Then
                    PixelValue(kk) = min
                End If

                If (PixelValue(kk) > max) Then
                    PixelValue(kk) = max
                End If

                scale2 = PixelValue(kk) - min
                iscale = CInt(scale2 * scale)
                Scaled_8Bit_ImageData(kk) = CByte(iscale)
                kk = kk + 1

            Next

        Next

        ij = Nothing
        jj = Nothing
        kk = Nothing
        min = Nothing
        max = Nothing
        scale = Nothing
        scale2 = Nothing
        PixelValue = Nothing

        Return Scaled_8Bit_ImageData

    End Function

Public Function CreateImageFromPixelData(ByVal ImageData_8Bit() As Byte) As Image

        Dim ImageFrom8BitPixelData As Image
        Dim Stride As Integer = 2048
        Dim ImagePalette As System.Drawing.Imaging.ColorPalette
        Dim _pImagePointer As IntPtr

        If _pImagePointer = IntPtr.Zero Then
            _pImagePointer = AllocHGlobal(3 * DisplayRowSize * DisplayCoulumnSize * 2) 'Maximum space for all resolutions (MCE-C013-U), it's also fine for B01
        End If

        Marshal.Copy(ImageData_8Bit, 0, _pImagePointer, Image_Resolution_Size)
        ImageFrom8BitPixelData = New Bitmap(Rows, Columns, Stride, System.Drawing.Imaging.PixelFormat.Format8bppIndexed, _pImagePointer)

        'For Format8bppIndexed, we have to construct a palette as following.
        ImagePalette = ImageFrom8BitPixelData.Palette

        For i As Integer = 0 To ImagePalette.Entries.Length - 1
            ImagePalette.Entries(i) = Color.FromArgb(MaxPixelValue_EightBitImg, i, i, i)
        Next

        ImageFrom8BitPixelData.Palette = ImagePalette

        Stride = Nothing
        ImagePalette = Nothing
        _pImagePointer = IntPtr.Zero

        Return ImageFrom8BitPixelData

    End Function

Public Function ScaleTo8BitAndDisplayImage(ByVal ImageData() As Byte, ByVal ImageWindowWidth As Integer, ByVal ImageWindowCenter As Integer) As Image
        Dim Scaled8Bit_ImageData(Image_Resolution_Size) As Byte

        'Scaled8Bit_ImageData = ImageScaling_16BitTo8Bit(ImageDataBuffer_InProcess, DICOM_Window_Width, DICOM_Window_Center)
        Scaled8Bit_ImageData = ImageScaling_16BitTo8Bit(ImageData, ImageWindowWidth, ImageWindowCenter)

        'Show the image
        BigpicImage = CreateImageFromPixelData(Scaled8Bit_ImageData)

        Scaled8Bit_ImageData = Nothing

        GC.Collect()

        Return BigpicImage

    End Function

Private Sub PicVImage_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PicVImage.MouseMove
 Application.DoEvents()
                            If Mouse_Down = True Then
                                deltaX = Convert.ToInt32((ptWLDown.X - e.X) * changeValWidth)
                                deltaY = Convert.ToInt32((ptWLDown.Y - e.Y) * changeValCentre)
                                winCentre -= deltaY
                                winWidth -= deltaX
                                If (winWidth < 1) Then
                                    winWidth = 1
                                End If
                                winMin = winCentre - (winWidth * 0.5)
                                winMax = winCentre + (winWidth * 0.5)
                           
                                ptWLDown.X = e.X
                                ptWLDown.Y = e.Y

                                PictureboxImage.Image = ScaleTo8BitAndDisplayImage(ImageDataBuffer_InProcess, winWidth, winCentre)

                      End if

End sub

Can anybody please tell what should I do release the memory which gets accumulated after every time i adjust brightness and contrast?

Thanks in advance.

Regards,
Susheelss

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>