17-10-12, 08:43 PM
كاتب الموضوع : silverlight
الاخوه الكرام مرفق كود لتحويل الصور الملونه الي صور ابيض واسود
الكود تقريبا كامل لمن يريد استخدامه ومرفق ايضا الملف بالفيجوال استوديو 2008
الكود ببساطه يعتمد علي فتح صوره معينه وعرضها داخل PictureBox ومن ثم باستخدام +GDI وتحديدا استخدام ColorMatrix يتم اعادة رسم الصوره مره اخري بالوان ابيض واسود وبعد ذلك يتم عرض الصوره باللون الابيض والاسود داخل PictureBox اخر ومن ثم يمكن حفظ الصوره علي الهارد ديسك
كود :
Public Class Form1
Private Sub btnOpenColorImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenColorImage.Click
Dim dlg As OpenFileDialog = New OpenFileDialog()
Dim imagFilter As String = "Image files (*.jpg,*.jpeg,*.bmp,*.gif,*.png)|*.BMP;*.PNG;*.JPG;*.JPEG;*.GIF"
dlg.Filter = imagFilter
dlg.RestoreDirectory = True
If dlg.ShowDialog() = DialogResult.OK Then
Dim img As Image = Image.FromFile(dlg.FileName)
PictureBox1.Image = img.GetThumbnailImage(PictureBox1.Width, PictureBox1.Height, Nothing, Nothing)
End If
End Sub
Private Sub btnConvertToGray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvertToGray.Click
If PictureBox1.Image Is Nothing Then
Dim msgString As String = "Load the Colored Image"
Dim msgCaption As String = "Error"
MessageBox.Show(msgString, msgCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
Dim grayImage As Image = PictureBox1.Image
Dim bm As Bitmap = New Bitmap(grayImage.Width, grayImage.Height)
Dim g As Graphics = Graphics.FromImage(bm)
Dim cm As System.Drawing.Imaging.ColorMatrix = New System.Drawing.Imaging.ColorMatrix(New Single()() _
{New Single() {0.3, 0.3, 0.3, 0, 0}, _
New Single() {0.59, 0.59, 0.59, 0, 0}, _
New Single() {0.11, 0.11, 0.11, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
Dim ia As System.Drawing.Imaging.ImageAttributes = New System.Drawing.Imaging.ImageAttributes()
ia.SetColorMatrix(cm)
g.DrawImage(grayImage, New Rectangle(0, 0, grayImage.Width, _
grayImage.Height), 0, 0, grayImage.Width, _
grayImage.Height, GraphicsUnit.Pixel, ia)
g.Dispose()
PictureBox2.Image = bm.GetThumbnailImage(PictureBox2.Width, PictureBox2.Height, Nothing, Nothing)
End If
End Sub
Private Sub btnSaveGrayImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveGrayImage.Click
Dim SaveFileDialog1 As New SaveFileDialog()
SaveFileDialog1.Filter = "Image files (*.BMP, *.JPG, *.GIF)|*.bmp;*.jpg;*.gif"
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
If Not PictureBox1.Image Is Nothing Then
PictureBox2.Image.Save(SaveFileDialog1.FileName)
End If
End If
End Sub
End Class
بالتوفيق
اخوكم عمر