|
Option Strict On
Option Explicit On
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices
Public Class Form1
Private Function Sharpen(ByVal InBitmap As Bitmap, _
ByVal Value As Double) As Bitmap
If Value < 0 Then Value = 0
If Value > 100 Then Value = 100
Value = Value / 50
Dim intX As New Integer
Dim intY As New Integer
Dim Bmp24Pos As New Integer
Dim Bmp24Pos1 As New Integer
Dim SharpenRed As New Double
Dim SharpenGreen As New Double
Dim SharpenBlue As New Double
Dim Bmp24Stride As Integer = ((InBitmap.Width * 3) + 3) And Not 3
Dim InData As Byte() = New Byte((InBitmap.Height * Bmp24Stride) - 1) {}
Dim OutData As Byte() = New Byte((InBitmap.Height * Bmp24Stride) - 1) {}
Dim hInData As GCHandle = GCHandle.Alloc(InData, GCHandleType.Pinned)
Dim Bmp24 As New Bitmap(InBitmap.Width, InBitmap.Height, _
Bmp24Stride, PixelFormat.Format24bppRgb, _
hInData.AddrOfPinnedObject())
Using Bmp24Gra As Graphics = Graphics.FromImage(Bmp24)
Bmp24Gra.DrawImageUnscaledAndClipped(InBitmap, _
New Rectangle(0, 0, Bmp24.Width, Bmp24.Height))
End Using
For intY = 0 To Bmp24.Height - 1
For intX = 0 To Bmp24.Width - 1
Bmp24Pos = (intY * Bmp24Stride) + (intX * 3)
SharpenRed = InData(Bmp24Pos + 2)
SharpenGreen = InData(Bmp24Pos + 1)
SharpenBlue = InData(Bmp24Pos + 0)
If (intY - 1) >= 0 Then
If (intX - 1) >= 0 Then
Bmp24Pos1 = ((intY - 1) * Bmp24Stride) + _
((intX - 1) * 3)
SharpenRed = Math.Abs(SharpenRed + (Value * _
(SharpenRed - InData(Bmp24Pos1 + 2))))
SharpenGreen = Math.Abs(SharpenGreen + (Value * _
(SharpenGreen - InData(Bmp24Pos1 + 1))))
SharpenBlue = Math.Abs(SharpenBlue + (Value * _
(SharpenBlue - InData(Bmp24Pos1 + 0))))
If SharpenRed > 255 Then SharpenRed = 255
If SharpenGreen > 255 Then SharpenGreen = 255
If SharpenBlue > 255 Then SharpenBlue = 255
End If
End If
OutData(Bmp24Pos + 2) = CByte(SharpenRed)
OutData(Bmp24Pos + 1) = CByte(SharpenGreen)
OutData(Bmp24Pos + 0) = CByte(SharpenBlue)
Next
Next
hInData.Free()
Dim hOutData As GCHandle = GCHandle.Alloc(OutData, GCHandleType.Pinned)
Dim NewBmp24BD As New BitmapData
NewBmp24BD.Width = Bmp24.Width
NewBmp24BD.Height = Bmp24.Height
NewBmp24BD.Stride = Bmp24Stride
NewBmp24BD.PixelFormat = Bmp24.PixelFormat
NewBmp24BD.Scan0 = hOutData.AddrOfPinnedObject()
Dim Bmp24BD As BitmapData = _
Bmp24.LockBits(New Rectangle(0, 0, Bmp24.Width, Bmp24.Height), _
ImageLockMode.WriteOnly Or ImageLockMode.UserInputBuffer, _
Bmp24.PixelFormat, NewBmp24BD)
Bmp24.UnlockBits(Bmp24BD)
hOutData.Free()
Return Bmp24
End Function
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
PictureBox1.Image = My.Resources.City005
PictureBox2.Image = My.Resources.City005
End Sub
Private Sub HScrollBar1_Scroll( _
ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.ScrollEventArgs) _
Handles HScrollBar1.Scroll
If Not PictureBox2.Image Is Nothing Then
PictureBox2.Image.Dispose()
End If
PictureBox2.Image = Sharpen( _
CType(PictureBox1.Image, Bitmap), HScrollBar1.Value)
End Sub
End Class |