Nasim_m
پنج شنبه 22 اردیبهشت 1390, 00:44 صبح
سلام
من یک PictureBox دارم و می خوام وقتی روش با موس قرار می گیرم سیاه و سفید بشه و وقتی موس رو از روش کنار میکشم دوباره رنگی بشه .
برای سیاه و سفید کردن از تابع زیر استفاده کردم:
Public Function grayscale(ByVal img As Image) As Image
Dim cm As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {0.299, 0.299, 0.299, 0, 0}, _
New Single() {0.587, 0.587, 0.587, 0, 0}, _
New Single() {0.114, 0.114, 0.114, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
Dim cm2 As ColorMatrix = New ColorMatrix(New Single()() {New Single() {0.299, 0.299, 0.299, 0, 0}, New Single() {0.587, 0.587, 0.587, 0, 0}, New Single() {0.114, 0.114, 0.114, 0, 0}, New Single() {0, 0, 0, 1, 0}, New Single() {0, 0, 0, 0, 1}})
Return draw_adjusted_image(img, cm)
End Function
Private Function draw_adjusted_image(ByVal img As Image, _
ByVal cm As ColorMatrix) As Image
Try
Dim bmp As New Bitmap(img) ' create a copy of the source image
Dim imgattr As New ImageAttributes()
Dim rc As New Rectangle(0, 0, img.Width, img.Height)
Dim g As Graphics = Graphics.FromImage(img)
' associate the ColorMatrix object with an ImageAttributes object
imgattr.SetColorMatrix(cm)
' draw the copy of the source image back over the original image,
'applying the ColorMatrix
g.DrawImage(bmp, rc, 0, 0, img.Width, img.Height, _
GraphicsUnit.Pixel, imgattr)
g.Dispose()
Return img
Catch
Return img
End Try
End Function
و برای اینکه تصویر اولیه رو از دست ندم قبل ار تغییر رنگ اونو توی یک image به نام pic کپی کردم
Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
pic = PictureBox1.Image
PictureBox1.Image = grayscale(PictureBox1.Image)
End Sub
Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
PictureBox1.Image = pic
End Sub
به نظر خودم همه چی درسته ولی مشکل اینجاست که عکسی که توی pic دخیره میشه عکسیه که سیاه و سفید شده در حالی که کد تغییر رنگ بعد از کپی کردنه:متفکر:
میشه کمکم کنید و بگید مشکل از کجاست
من یک PictureBox دارم و می خوام وقتی روش با موس قرار می گیرم سیاه و سفید بشه و وقتی موس رو از روش کنار میکشم دوباره رنگی بشه .
برای سیاه و سفید کردن از تابع زیر استفاده کردم:
Public Function grayscale(ByVal img As Image) As Image
Dim cm As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {0.299, 0.299, 0.299, 0, 0}, _
New Single() {0.587, 0.587, 0.587, 0, 0}, _
New Single() {0.114, 0.114, 0.114, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
Dim cm2 As ColorMatrix = New ColorMatrix(New Single()() {New Single() {0.299, 0.299, 0.299, 0, 0}, New Single() {0.587, 0.587, 0.587, 0, 0}, New Single() {0.114, 0.114, 0.114, 0, 0}, New Single() {0, 0, 0, 1, 0}, New Single() {0, 0, 0, 0, 1}})
Return draw_adjusted_image(img, cm)
End Function
Private Function draw_adjusted_image(ByVal img As Image, _
ByVal cm As ColorMatrix) As Image
Try
Dim bmp As New Bitmap(img) ' create a copy of the source image
Dim imgattr As New ImageAttributes()
Dim rc As New Rectangle(0, 0, img.Width, img.Height)
Dim g As Graphics = Graphics.FromImage(img)
' associate the ColorMatrix object with an ImageAttributes object
imgattr.SetColorMatrix(cm)
' draw the copy of the source image back over the original image,
'applying the ColorMatrix
g.DrawImage(bmp, rc, 0, 0, img.Width, img.Height, _
GraphicsUnit.Pixel, imgattr)
g.Dispose()
Return img
Catch
Return img
End Try
End Function
و برای اینکه تصویر اولیه رو از دست ندم قبل ار تغییر رنگ اونو توی یک image به نام pic کپی کردم
Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
pic = PictureBox1.Image
PictureBox1.Image = grayscale(PictureBox1.Image)
End Sub
Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
PictureBox1.Image = pic
End Sub
به نظر خودم همه چی درسته ولی مشکل اینجاست که عکسی که توی pic دخیره میشه عکسیه که سیاه و سفید شده در حالی که کد تغییر رنگ بعد از کپی کردنه:متفکر:
میشه کمکم کنید و بگید مشکل از کجاست