I found this code for sharpening images online. I haven't done much testing with it yet, but i do know it's ruining the transparent parts of my bitmaps.
Can anyone give a solution, that retains the transparent pixels?
Code:Imports System.Runtime.InteropServices Imports System.Drawing.Imaging Public Class Convolution Public Sub Convolution3x3(ByRef bmp As Bitmap) Dim Factor As Integer = Matrix.Factor If Factor = 0 Then Return End If Dim TopLeft As Integer = Matrix.TopLeft Dim TopMid As Integer = Matrix.TopMid Dim TopRight As Integer = Matrix.TopRight Dim MidLeft As Integer = Matrix.MidLeft Dim MidRight As Integer = Matrix.MidRight Dim BottomLeft As Integer = Matrix.BottomLeft Dim BottomMid As Integer = Matrix.BottomMid Dim BottomRight As Integer = Matrix.BottomRight Dim Pixel As Integer = Matrix.Pixel Dim Offset As Integer = Matrix.Offset Dim TempBmp As Bitmap = bmp.Clone() Dim bmpData As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb) Dim TempBmpData As BitmapData = TempBmp.LockBits(New Rectangle(0, 0, TempBmp.Width, TempBmp.Height), ImageLockMode.[ReadOnly], PixelFormat.Format24bppRgb) Dim ptr As IntPtr = bmpData.Scan0 Dim TempPtr As IntPtr = TempBmpData.Scan0 Dim Pix As Integer = 0 Dim Stride As Integer = bmpData.Stride Dim DoubleStride As Integer = Stride * 2 Dim Width As Integer = bmp.Width - 2 Dim Height As Integer = bmp.Height - 2 Dim stopAddress As Integer = CInt(CInt(ptr)) + bmpData.Stride * bmpData.Height For y As Integer = 0 To Height - 1 For x As Integer = 0 To Width - 1 Pix = (((((Marshal.ReadByte(CInt(TempPtr) + 2) * TopLeft) + (Marshal.ReadByte(CInt(TempPtr) + 5) * TopMid) + (Marshal.ReadByte(CInt(TempPtr) + 8) * TopRight)) + _ ((Marshal.ReadByte(CInt(TempPtr) + 2 + Stride) * MidLeft) + (Marshal.ReadByte(CInt(TempPtr) + 5 + Stride) * Pixel) + (Marshal.ReadByte(CInt(TempPtr) + 8 + Stride) * MidRight)) + _ ((Marshal.ReadByte(CInt(TempPtr) + 2 + DoubleStride) * BottomLeft) + (Marshal.ReadByte(CInt(TempPtr) + 5 + DoubleStride) * BottomMid) + (Marshal.ReadByte(CInt(TempPtr) + 8 + DoubleStride) * BottomRight))) \ Factor) + Offset) If Pix < 0 Then Pix = 0 ElseIf Pix > 255 Then Pix = 255 End If Marshal.WriteByte(CInt(ptr) + 5 + Stride, CByte(Pix)) Pix = (((((Marshal.ReadByte(CInt(TempPtr) + 1) * TopLeft) + (Marshal.ReadByte(CInt(TempPtr) + 4) * TopMid) + (Marshal.ReadByte(CInt(TempPtr) + 7) * TopRight)) + _ ((Marshal.ReadByte(CInt(TempPtr) + 1 + Stride) * MidLeft) + (Marshal.ReadByte(CInt(TempPtr) + 4 + Stride) * Pixel) + (Marshal.ReadByte(CInt(TempPtr) + 7 + Stride) * MidRight)) + _ ((Marshal.ReadByte(CInt(TempPtr) + 1 + DoubleStride) * BottomLeft) + (Marshal.ReadByte(CInt(TempPtr) + 4 + DoubleStride) * BottomMid) + (Marshal.ReadByte(CInt(TempPtr) + 7 + DoubleStride) * BottomRight))) \ Factor) + Offset) If Pix < 0 Then Pix = 0 ElseIf Pix > 255 Then Pix = 255 End If Marshal.WriteByte(CInt(ptr) + 4 + Stride, CByte(Pix)) Pix = (((((Marshal.ReadByte(CInt(TempPtr)) * TopLeft) + (Marshal.ReadByte(CInt(TempPtr) + 3) * TopMid) + (Marshal.ReadByte(CInt(TempPtr) + 6) * TopRight)) + _ ((Marshal.ReadByte(CInt(TempPtr) + Stride) * MidLeft) + (Marshal.ReadByte(CInt(TempPtr) + 3 + Stride) * Pixel) + (Marshal.ReadByte(CInt(TempPtr) + 6 + Stride) * MidRight)) + _ ((Marshal.ReadByte(CInt(TempPtr) + DoubleStride) * BottomLeft) + (Marshal.ReadByte(CInt(TempPtr) + 3 + DoubleStride) * BottomMid) + (Marshal.ReadByte(CInt(TempPtr) + 6 + DoubleStride) * BottomRight))) \ Factor) + Offset) If Pix < 0 Then Pix = 0 ElseIf Pix > 255 Then Pix = 255 End If Marshal.WriteByte(CInt(ptr) + 3 + Stride, CByte(Pix)) ptr = CInt(ptr) + 3 TempPtr = CInt(TempPtr) + 3 Next Next bmp.UnlockBits(bmpData) TempBmp.UnlockBits(TempBmpData) End Sub Public Property Matrix() As ConvolutionMatrix Get Return m_Matrix End Get Set(ByVal value As ConvolutionMatrix) m_Matrix = value End Set End Property Private m_Matrix As ConvolutionMatrix End ClassCode:Public Class ConvolutionMatrix Public Sub New() Pixel = 1 Factor = 1 End Sub Public Sub Apply(ByVal Val As Integer) TopLeft = Val TopMid = Val TopRight = Val MidLeft = Val MidRight = Val BottomLeft = Val BottomMid = Val BottomRight = Val Pixel = Val End Sub Public Property TopLeft() As Integer Get Return m_TopLeft End Get Set(ByVal value As Integer) m_TopLeft = value End Set End Property Private m_TopLeft As Integer Public Property TopMid() As Integer Get Return m_TopMid End Get Set(ByVal value As Integer) m_TopMid = value End Set End Property Private m_TopMid As Integer Public Property TopRight() As Integer Get Return m_TopRight End Get Set(ByVal value As Integer) m_TopRight = value End Set End Property Private m_TopRight As Integer Public Property MidLeft() As Integer Get Return m_MidLeft End Get Set(ByVal value As Integer) m_MidLeft = value End Set End Property Private m_MidLeft As Integer Public Property MidRight() As Integer Get Return m_MidRight End Get Set(ByVal value As Integer) m_MidRight = value End Set End Property Private m_MidRight As Integer Public Property BottomLeft() As Integer Get Return m_BottomLeft End Get Set(ByVal value As Integer) m_BottomLeft = value End Set End Property Private m_BottomLeft As Integer Public Property BottomMid() As Integer Get Return m_BottomMid End Get Set(ByVal value As Integer) m_BottomMid = value End Set End Property Private m_BottomMid As Integer Public Property BottomRight() As Integer Get Return m_BottomRight End Get Set(ByVal value As Integer) m_BottomRight = value End Set End Property Private m_BottomRight As Integer Public Property Pixel() As Integer Get Return m_Pixel End Get Set(ByVal value As Integer) m_Pixel = value End Set End Property Private m_Pixel As Integer Public Property Factor() As Integer Get Return m_Factor End Get Set(ByVal value As Integer) m_Factor = value End Set End Property Private m_Factor As Integer Public Property Offset() As Integer Get Return m_Offset End Get Set(ByVal value As Integer) m_Offset = value End Set End Property Private m_Offset As Integer End ClassCode:Public Shared Sub ApplySharpen(ByRef bmp As Bitmap, ByVal weight As Integer) Dim m As New ConvolutionMatrix() m.Apply(0) m.Pixel = weight m.TopMid = -2 m.MidLeft = -2 m.MidRight = -2 m.BottomMid = -2 m.Factor = weight - 8 Dim C As New Convolution() C.Matrix = m C.Convolution3x3(bmp) End Sub




Reply With Quote