i have seen so many programs in vb.net for applying emboss effect to a picture
when I run those programs,they start to applying emboss effect on the picture
but they don't ask for any input from user so that he can input desire value and can get desire result.
now i want to build such an application which can get input from the user and can give desire results to the user.
i have two form,form1 and form2
form1 contains
1. A file menu through which we can open the image
2. a filter menu through which we can show form2
3. a picture box on which picture will be displayed
form2 contains
1.a picture box
2.a progress bar which shows the progress when effect is being applied to the picture box
3. a NumericUpDown control through which i can set the value of emboss effect.
4. an apply button by pressing it effect is started to being applied.
5. a Ok button by pressing it form2 closes and effect is applied back to original image and that image updated.
apply button code is as follow
Code:
Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
Dim bmap As Bitmap
bmap = New Bitmap(picEmboss.Image)
picEmboss.Image = bmap
Dim tempbmp As New Bitmap(picEmboss.Image)
Dim i, j As Integer
Dim DispX As Integer = 1, DispY As Integer = 1
Dim red, green, blue As Integer
With tempbmp
For i = 0 To .Height - 2
For j = 0 To .Width - 2
Dim pixel1, pixel2 As System.Drawing.Color
pixel1 = .GetPixel(j, i)
pixel2 = .GetPixel(j + DispX, i + DispY)
red = Math.Min(Math.Abs(CInt(pixel1.R) - CInt(pixel2.R)) + 128, 255)
green = Math.Min(Math.Abs(CInt(pixel1.G) - CInt(pixel2.G)) + 128, 255)
blue = Math.Min(Math.Abs(CInt(pixel1.B) - CInt(pixel2.B)) + 128, 255)
bmap.SetPixel(j, i, Color.FromArgb(red, green, blue))
Next
If i Mod 10 = 0 Then
picEmboss.Invalidate()
picEmboss.Refresh()
Me.Text = Int(100 * i / (picEmboss.Image.Height - 2)).ToString & "%"
pbEmboss.Value = Int(100 * i / (picEmboss.Image.Height - 2))
End If
Next
End With
picEmboss.Refresh()
Me.Text = "Done embossing image"
End Sub
i want to update the program as follow
program can take input from the user and the apply the emboss effect according to given input through numeric up down control
i can be to stop the effect any time through an other button 'stop'
i want that perventage should be on the progress bar.
these modifications i want to do but i don't know how to do that???
any body can help me
i m sending attachment.......thanks
Add a CheckBox1 to your form 2. Set his .appearance property to Button, and .text="Stop".
Then add this red code:
Originally Posted by aamir55
...
Code:
Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
Dim bmap As Bitmap
bmap = New Bitmap(picEmboss.Image)
picEmboss.Image = bmap
Dim tempbmp As New Bitmap(picEmboss.Image)
Dim i, j As Integer
Dim DispX As Integer = 1, DispY As Integer = 1
Dim red, green, blue As Integer
With tempbmp
For i = 0 To .Height - 2
For j = 0 To .Width - 2
Dim pixel1, pixel2 As System.Drawing.Color
pixel1 = .GetPixel(j, i)
pixel2 = .GetPixel(j + DispX, i + DispY)
red = Math.Min(Math.Abs(CInt(pixel1.R) - CInt(pixel2.R)) + 128, 255)
green = Math.Min(Math.Abs(CInt(pixel1.G) - CInt(pixel2.G)) + 128, 255)
blue = Math.Min(Math.Abs(CInt(pixel1.B) - CInt(pixel2.B)) + 128, 255)
bmap.SetPixel(j, i, Color.FromArgb(red, green, blue))
Next
If i Mod 10 = 0 Then
picEmboss.Invalidate()
picEmboss.Refresh()
Me.Text = Int(100 * i / (picEmboss.Image.Height - 2)).ToString & "%"
pbEmboss.Value = Int(100 * i / (picEmboss.Image.Height - 2))
End If
Application.DoEvents() ' To give the user the opportunity to press stop
If CheckBox1.Checked then
CheckBox1.Checked = false
'Now, code to stop
'Exit For, Exit Sub, i =.Height, Whatever you want or like
end if
Next
End With
picEmboss.Refresh()
Me.Text = "Done embossing image"
End Sub
thanks when i press stop then effect being applied stops and when i again press apply button then effect starts from begining
i want that effect should be started from there where i stoped it
and i also want that progress should be seen in the progress instead of in the title bar of the form........thanks
thanks when i press stop then effect being applied stops and when i again press apply button then effect starts from begining
i want that effect should be started from there where i stoped it
Oh, You should have started saying it.
Is easy. If you declare a variable as static, then, the sub will remember his older value, when you run it again.
Add this variable:
Code:
Static Former_i as integer=0 ' First time it is run, his value = 0
Originally Posted by aamir55
Then change your For i line to:
Code:
Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
Dim bmap As Bitmap
bmap = New Bitmap(picEmboss.Image)
picEmboss.Image = bmap
Dim tempbmp As New Bitmap(picEmboss.Image)
Dim i, j As Integer
Dim DispX As Integer = 1, DispY As Integer = 1
Dim red, green, blue As Integer
Static Former_i As Integer = 0
With tempbmp
For i = Former_i To .Height - 2
For j = 0 To .Width - 2
Dim pixel1, pixel2 As System.Drawing.Color
pixel1 = .GetPixel(j, i)
pixel2 = .GetPixel(j + DispX, i + DispY)
red = Math.Min(Math.Abs(CInt(pixel1.R) - CInt(pixel2.R)) + 128, 255)
green = Math.Min(Math.Abs(CInt(pixel1.G) - CInt(pixel2.G)) + 128, 255)
blue = Math.Min(Math.Abs(CInt(pixel1.B) - CInt(pixel2.B)) + 128, 255)
bmap.SetPixel(j, i, Color.FromArgb(red, green, blue))
Next
If i Mod 10 = 0 Then
picEmboss.Invalidate()
picEmboss.Refresh()
Me.Text = Int(100 * i / (picEmboss.Image.Height - 2)).ToString & "%"
pbEmboss.Value = Int(100 * i / (picEmboss.Image.Height - 2))
End If
Application.DoEvents() ' To give the user the opportunity to press stop
If CheckBox1.Checked Then
CheckBox1.Checked = False
'Now, code to stop
Former_i = i + 1 'It will be remembered because is static
Exit For ' It exits the For i ... Next
End If
Next
End With
picEmboss.Refresh()
Me.Text = "Done embossing image"
End Sub
and i also want that progress should be seen in the progress instead of in the title bar of the form........thanks
thanks but i want that when i press stop button then it should convert into start button and again pressing it,effect should started there where it was stoped
and i have not click apply button after pressing stop button
modified code as follow
Code:
If chkStop.Checked Then
chkStop.Checked = False
chkStop.Text = "Start"
former = i + 1
Exit Sub
End If
The Static keyword does just that. If it has a value, that is static, and if it doesn't already have a value, it is set to 0. Not like DIM which will reset EVERY time it's called.
thanks but i want that when i press stop button then it should convert into start button and again pressing it,effect should started there where it was stoped
and i have not click apply button after pressing stop button
¿Did you mean "automatically again pressing it", or "manually again pressing it"?
I cannot tell if you want a Pause, or a Stop button.
¿What if you want to discard changes, and start again?
Originally Posted by aamir55
Code:
If chkStop.Checked Then
chkStop.Checked = False
chkStop.Text = "Start"
former = i + 1
Exit Sub
End If
...
I had only give you example code as illustration. Take in account that, if you want to completely restart the emboss process, you need to re-set former_i to 0.
Code:
If chkStop.Checked Then
chkStop.Checked = False
chkStop.Text = "Start"
former = i + 1
Exit Sub ' WARNING: Exit Sub avoids picEmboss.Refresh()
ElseIf i >= .Height - 2 orelse former >= .Height - 2 Then 'This line needs to be after Next i instruction. Otherwise is buggy
former = 0 ' To be able to restart
End If 'chkStop.Checked
i have added ur code but program was giving output well and i have to press apply button again and again to give the effect to the picture box
i want to start the effect where it was stoped
but i want to stop it manualy,on pressing start button
when i press apply button first time then chck box converts in to stop button and similarly pressing stop button it converts into start button
now i want that effect should started where it was soped but not from beggining
i want to ask that why exit sub avoids picEmboss.refresh()
sending u modified attachment
i have added ur code but program was giving output well and i have to press apply button again and again to give the effect to the picture box
i want to start the effect where it was stoped
but i want to stop it manualy,on pressing start button
when i press apply button first time then chck box converts in to stop button and similarly pressing stop button it converts into start button
now i want that effect should started where it was soped but not from beggining
i want to ask that why exit sub avoids picEmboss.refresh()
sending u modified attachment
my bad...
replace chkStop with a button named btnPause; add this code:
Code:
Private Sub btnPause_Pressed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click
With btnPause
Select Case .Text
Case "Stop"
StopEmbossing = True
.Text = "Restart"
Case "Restart"
StopEmbossing = False
.Text = "Stop"
btnApply_Click(Nothing, Nothing)
End Select
End With
End Sub
then replace your btnApply_Click sub with:
Code:
Private StopEmbossing As Boolean = False
Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
btnOk.Enabled = False
btnPause.Focus()
Dim bmap As New Bitmap(picEmboss.Image)
picEmboss.Image = bmap
'Needed variables
Dim tempbmp As New Bitmap(picEmboss.Image)
Dim i, j As Integer
Dim DispX As Integer = 1, DispY As Integer = 1
Dim red, green, blue As Integer
Static former As Integer = 0 'first time it is run,its value is zero
'So, we go across the image matrix, pixel by pixel
With tempbmp
For i = former To .Height - 2 'rows
For j = 0 To .Width - 2 'cols
Dim pixel1, pixel2 As System.Drawing.Color
'gets pixel and next one
pixel1 = .GetPixel(j, i)
pixel2 = .GetPixel(j + DispX, i + DispY)
'We change colors
red = Math.Min(Math.Abs(CInt(pixel1.R) - _
CInt(pixel2.R)) + 128, 255)
green = Math.Min(Math.Abs(CInt(pixel1.G) - _
CInt(pixel2.G)) + 128, 255)
blue = Math.Min(Math.Abs(CInt(pixel1.B) - _
CInt(pixel2.B)) + 128, 255)
bmap.SetPixel(j, i, Color.FromArgb(red, green, blue))
Next
'Dim x As Integer = (Me.pbEmboss.Width / 2) - (sz.Width / 2)
'Dim y As Integer = (Me.pbEmboss.Height / 2) - (sz.Height / 2)
'Every 10 rows shows current result
If i Mod 10 = 0 Then
picEmboss.Invalidate()
picEmboss.Refresh()
picEmboss.Invalidate()
picEmboss.Refresh()
Me.Text = Int(100 * i / (picEmboss.Image.Height - 2)).ToString & "%"
pbEmboss.Value = Int(100 * i / (picEmboss.Image.Height - 2))
End If
Application.DoEvents()
If StopEmbossing Then
btnOk.Enabled = True
former = i + 1 'It will be remembered because is static
Exit Sub
ElseIf i >= .Height - 2 OrElse former >= .Height - 2 Then
former = 0
End If
'chkStop.Checked = True
Next
End With
'Refresh the image and sets the mark in the title
Me.Text = "Done"
picEmboss.Refresh()
btnOk.Enabled = True
btnOk.Focus()
End Sub
...
i want to ask that why exit sub avoids picEmboss.refresh()
...
because Exit Sub exits the sub right away, without executing the rest of your code. You have codded a line picEmboss.refresh() after the last next instruction. Obviously is not executed.
thanks..... my problem solved
few more things if u can help me
1.i want to show progress in the progress bar instead of on the title bar
i mean i can show percentage in the center of the progress bar
2.when i press apply button then emboss strength is full
i want to minimize it
it takes much time on big image and less time on a small image
i want to chages such that user can enter a strength value and then apply button and effect should be applied according to this entered value
for this purpose i want to use text boxes or track bar to change emboss effect strength
so user can taste differnt emboss effect qualities
thanks
thanks for giving me depth knowledge how to stop and resume emboss effect
i want to do this for other three effects...these are Sharpen,Smooth and Difuse
i want to just add functionality which i have made in emboss effect but i don't know how to add that functionality(Stop and Start) on these effects
Emboss Effect...u have helped me to do so
Code:
Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
btnOk.Enabled = False
btnPause.Focus()
Dim bmap As New Bitmap(picEmboss.Image)
picEmboss.Image = bmap
'Needed variables
Dim tempbmp As New Bitmap(picEmboss.Image)
Dim i, j As Integer
Dim DispX As Integer = 1, DispY As Integer = 1
Dim red, green, blue As Integer
Static former As Integer = 0 'first time it is run,its value is zero
'So, we go across the image matrix, pixel by pixel
With tempbmp
For i = former To .Height - 2 'rows
For j = 0 To .Width - 2 'cols
Dim pixel1, pixel2 As System.Drawing.Color
'gets pixel and next one
pixel1 = .GetPixel(j, i)
pixel2 = .GetPixel(j + DispX, i + DispY)
'We change colors
red = Math.Min(Math.Abs(CInt(pixel1.R) - _
CInt(pixel2.R)) + 128, 255)
green = Math.Min(Math.Abs(CInt(pixel1.G) - _
CInt(pixel2.G)) + 128, 255)
blue = Math.Min(Math.Abs(CInt(pixel1.B) - _
CInt(pixel2.B)) + 128, 255)
bmap.SetPixel(j, i, Color.FromArgb(red, green, blue))
Next
'Dim x As Integer = (Me.pbEmboss.Width / 2) - (sz.Width / 2)
'Dim y As Integer = (Me.pbEmboss.Height / 2) - (sz.Height / 2)
'Every 10 rows shows current result
If i Mod 10 = 0 Then
picEmboss.Invalidate()
picEmboss.Refresh()
picEmboss.Invalidate()
picEmboss.Refresh()
Me.Text = Int(100 * i / (picEmboss.Image.Height - 2)).ToString & "%"
pbEmboss.Value = Int(100 * i / (picEmboss.Image.Height - 2))
End If
Application.DoEvents()
If StopEmbossing Then
btnOk.Enabled = True
former = i + 1 'It will be remembered because is static
Exit Sub
ElseIf i >= .Height - 2 OrElse former >= .Height - 2 Then
former = 0
End If
Next
End With
'Refresh the image and sets the mark in the title
Me.Text = "Done"
picEmboss.Refresh()
btnOk.Enabled = True
btnOk.Focus()
End Sub
Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click
With btnPause
Select Case .Text
Case "Stop"
StopEmbossing = True
.Text = "Restart"
Case "Restart"
StopEmbossing = False
.Text = "Stop"
btnApply_Click(Nothing, Nothing)
End Select
End With
End Sub
i want to add above functionality of start and stop on the following three effects
Sharpen Effect
Code:
Private Sub MnuSharpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuSharpen.Click
undoImage = MyPicture.Image
MnuUndo.Enabled = True
Dim bmap As New Bitmap(MyPicture.Image)
MyPicture.Image = bmap
Dim tempbmp As New Bitmap(MyPicture.Image)
Dim i, j As Integer
Dim DX As Integer = 1, DY As Integer = 1
Dim red, green, blue As Integer
With tempbmp
For i = DX To .Height - DX - 1
For j = DY To .Width - DY - 1
red = CInt(.GetPixel(j, i).R) + 0.5 * CInt((.GetPixel(j, i).R) - CInt(.GetPixel(j - DX, i - DY).R))
green = CInt(.GetPixel(j, i).G) + 0.7 * CInt((.GetPixel(j, i).G) - CInt(.GetPixel(j - DX, i - DY).G))
blue = CInt(.GetPixel(j, i).B) + 0.5 * CInt((.GetPixel(j, i).B - CInt(.GetPixel(j - DX, i - DY).B)))
red = Math.Min(Math.Max(red, 0), 255)
green = Math.Min(Math.Max(green, 0), 255)
blue = Math.Min(Math.Max(blue, 0), 255)
bmap.SetPixel(j, i, Color.FromArgb(red, green, blue))
Next
If i Mod 10 = 0 Then
MyPicture.Invalidate()
MyPicture.Refresh()
MyPicture.CreateGraphics.DrawString _
("Now Processing... " & Int(100 * i / (MyPicture.Image.Height - 2)).ToString _
& "%", New Font("Verdana", 12, FontStyle.Bold), Brushes.White, 10, 25)
End If
Next
End With
MyPicture.Refresh()
End Sub
Smooth Effect
Code:
Private Sub MnuSmooth_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuSmooth.Click
undoImage = MyPicture.Image
MnuUndo.Enabled = True
Dim bmap As New Bitmap(MyPicture.Image)
MyPicture.Image = bmap
Dim tempbmp As New Bitmap(MyPicture.Image)
Dim DX As Integer = 1
Dim DY As Integer = 1
Dim red, green, blue As Integer
Dim i, j As Integer
With tempbmp
For i = DX To .Height - DX - 1
For j = DY To .Width - DY - 1
red = CInt((CInt(.GetPixel(j - 1, i - 1).R) + _
CInt(.GetPixel(j - 1, i).R) + _
CInt(.GetPixel(j - 1, i + 1).R) + _
CInt(.GetPixel(j, i - 1).R) + _
CInt(.GetPixel(j, i).R) + _
CInt(.GetPixel(j, i + 1).R) + _
CInt(.GetPixel(j + 1, i - 1).R) + _
CInt(.GetPixel(j + 1, i).R) + _
CInt(.GetPixel(j + 1, i + 1).R)) / 9)
green = CInt((CInt(.GetPixel(j - 1, i - 1).G) + _
CInt(.GetPixel(j - 1, i).G) + _
CInt(.GetPixel(j - 1, i + 1).G) + _
CInt(.GetPixel(j, i - 1).G) + _
CInt(.GetPixel(j, i).G) + _
CInt(.GetPixel(j, i + 1).G) + _
CInt(.GetPixel(j + 1, i - 1).G) + _
CInt(.GetPixel(j + 1, i).G) + _
CInt(.GetPixel(j + 1, i + 1).G)) / 9)
blue = CInt((CInt(.GetPixel(j - 1, i - 1).B) + _
CInt(.GetPixel(j - 1, i).B) + _
CInt(.GetPixel(j - 1, i + 1).B) + _
CInt(.GetPixel(j, i - 1).B) + _
CInt(.GetPixel(j, i).B) + _
CInt(.GetPixel(j, i + 1).B) + _
CInt(.GetPixel(j + 1, i - 1).B) + _
CInt(.GetPixel(j + 1, i).B) + _
CInt(.GetPixel(j + 1, i + 1).B)) / 9)
red = Math.Min(Math.Max(red, 0), 255)
green = Math.Min(Math.Max(green, 0), 255)
blue = Math.Min(Math.Max(blue, 0), 255)
bmap.SetPixel(j, i, Color.FromArgb(red, green, blue))
Next
If i Mod 10 = 0 Then
MyPicture.Invalidate()
MyPicture.Refresh()
MyPicture.CreateGraphics.DrawString _
("Now Processing... " & Int(100 * i / (MyPicture.Image.Height - 2)).ToString _
& "%", New Font("Verdana", 12, FontStyle.Bold), Brushes.White, 10, 25)
End If
Next
End With
MyPicture.Refresh()
End Sub
Diffuse Effect
Code:
Private Sub MnuDiffuse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuDiffuse.Click
undoImage = MyPicture.Image
MnuUndo.Enabled = True
Dim bmap As New Bitmap(MyPicture.Image)
MyPicture.Image = bmap
Dim tempbmp As New Bitmap(MyPicture.Image)
Dim i As Integer, j As Integer
Dim DX As Integer
Dim DY As Integer
Dim red As Integer, green As Integer, blue As Integer
With tempbmp
For i = 3 To .Height - 3
For j = 3 To .Width - 3
DX = Rnd() * 4 - 2
DY = Rnd() * 4 - 2
red = .GetPixel(j + DX, i + DY).R
green = .GetPixel(j + DX, i + DY).G
blue = .GetPixel(j + DX, i + DY).B
bmap.SetPixel(j, i, Color.FromArgb(red, green, blue))
Next
If i Mod 10 = 0 Then
MyPicture.Invalidate()
MyPicture.Refresh()
MyPicture.CreateGraphics.DrawString _
("Now Processing... " & Int(100 * i / (MyPicture.Image.Height - 2)).ToString _
& "%", New Font("Verdana", 12, FontStyle.Bold), SystemBrushes.Control, 10, 20)
End If
Next
End With
MyPicture.Refresh()
End Sub
kindly help me apply same functionality to above three effects which u have helped me in aplying eboss effect
thanks
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.