Inserting Bitmap into array thus allowing extraction of pixel from the array and finally taking out the RGB Colors. How do i do that?
-
Friday, March 19, 2010 3:42 AM
Hi guys my task to switch the positions of pixels to distort an image
i heard when you want to switch a pixel with pixel you have to switch the rgb colors thats why i thought need the rgb value of the pixel. is that the correct direction for me to work on?
for example i want to switch position A with position B i would need another variable such as position C so that when i switch from position A to position B my position A value wont be lost
such as A = 2
B = 3
C = unknowni move C = A
A = B
B = Cis that the correct way? please advise. I know how to work out in theory but i am very poor with Visual Basic therefore need help with coding
i need help with inserting Bitmap into array I have done that part but i do not know if that is correct
this will allow me extraction of pixel from the array and finally taking out the RGB Colors and swapping the RGB colors to switch the pixels position
Below is the codes that i am having so far
Imports
System.Drawing.Imaging
Imports
System.Runtime.InteropServices
Public
Class Form1
Public Function CopyDataToBitmap(ByVal data As Byte()) As Bitmap
'Here create the Bitmap to the know height, width and format
Dim bmp As New Bitmap(640, 480, PixelFormat.Format24bppRgb)
'Create a BitmapData and Lock all pixels to be written
Dim bmpData As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.[WriteOnly], bmp.PixelFormat)
'Copy the data from the byte array into BitmapData.Scan0
Marshal.Copy(data, 0, bmpData.Scan0, data.Length)
'Unlock the pixels
bmp.UnlockBits(bmpData)
'Return the bitmap
Return bmp
End Function
Dim I1 As Bitmap
Dim Cx, Cy, TF, mp, o(256), grn(256), blue(256), red(256) As Integer
Dim I4 As Bitmap
Dim a As Integer = 0
Dim check As Boolean = False
'Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Dim I3 As Bitmap = New Bitmap(I1.Width, I1.Height)
'Dim I4 As Bitmap = New Bitmap(I1.Width, I1.Height)
'End Sub
Private Function CropBitmap(ByRef bmp As Bitmap, ByVal cropX As Integer, ByVal cropY As Integer, ByVal cropWidth As Integer, ByVal cropHeight As Integer) As Bitmap
Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight)
Dim cropped As Bitmap = bmp.Clone(rect, bmp.PixelFormat)
Return cropped
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ReadingFileDialog As New OpenFileDialog
Dim fileOpened As Boolean = False
If fileOpened = True Then
PictureBox1.Image.Dispose()
fileOpened =
False
End If
ReadingFileDialog.Title =
"Open My File"
' Restrict our choice of files to *.jpeg files.
ReadingFileDialog.Filter =
"Bitmap *.bmp|*.bmp|JPEG *.jpg|*.jpg|GIF *.gif|*.gif"
If ReadingFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
'ReadingFileDialog.ShowDialog()
fileOpened =
True
Dim IPB As Bitmap = New Bitmap(ReadingFileDialog.FileName)
If ReadingFileDialog.FileName <> "" Then
PictureBox1.Image =
New Bitmap(ReadingFileDialog.FileName)
End If
End If
I1 = PictureBox1.Image
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim Cx, Cy, o(256), grn(256), blue(256), red(256) As Integer
Dim o1(256), r1(256), b1(256), g1(256) As Integer 'For histogram & rect draw box
Dim PHX(1240)
Dim DFR(1280, 1024), DFG(1280, 1024), DFB(1280, 1024), PHV(1600, 1200), HCX(1024) As String
Dim DFRT(1280, 1024), DFGT(1280, 1024), DFBT(1280, 1024) As Integer '### added temp color channels
Dim CT As Color ' color object
Dim wow As Boolean = False
'Dim LMP As Point = MousePosition
Dim I3 As Bitmap
'= New Bitmap(I1.Width, I1.Height)`
' PictureBox1.Image = CropBitmap(I4, PictureBox1.Left, PictureBox1.Top, I4.Width / 2, I4.Height / 2)
'Code for getting percentage of comparing Cy,Cx to I1
' For Cx = 0 To I1.Width - 1
'For Cy = 0 To I1.Height - 1
'I1 = PictureBox1.Image
'CT = I1.GetPixel(20, 20) 'get the pixel data from the bitmap pixel
'PB2.Text = CInt(((Cy + 1) / I1.Height) * 100) & "%"
'DFR(20, 20) = CT.R 'DFR() is string (8-bit)
'PB2.Text = I1.Width
'DFG(Cx, Cy) = CT.G
'PB1.Text = DFG(Cx, Cy)
'DFB(Cx, Cy) = I1.Height
'PB1.Text = DFB(Cx, Cy)
'PB1.Text = (((CInt(Cx) + 1) / I1.Width) * 100) & "%"
'Next
'Next
'I3(0, 0) = I4
' I3(0, 1) = New Bitmap(I4.Width, I4.Height, 400, Drawing.Imaging.PixelFormat.Format24bppRgb, DFR(20, 20))
'PictureBox1.Image = I3(0, 0)
'PictureBox1.Height = I3(0, 0).Height
'PictureBox1.Width = I3(0, 0).Width
' Dim x, y As Integer
' 'Get color of the pixel
' For x = 1 To I1.Height
'For y = 1 To I1.Width
'Dim B(x, y) As Color
'A(x, y) = CT.R
'B(x, y) = CT
'B(x + 1, y + 1) = B(x, y)
'Next
'Next
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim d As New Drawing.Bitmap(1, 1)
Dim b As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(d)
b.CopyFromScreen(
New Drawing.Point(MousePosition.X, MousePosition.Y), New Drawing.Point(0, 0), d.Size)
Dim c As Drawing.Color = d.GetPixel(0, 0)
PictureBox1.BackColor = c
PB2.Text = PictureBox1.BackColor.Name
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
End Sub
Public Sub GetPixel_Example(ByVal e As PaintEventArgs)
' Create a Bitmap object from an image file.
Dim myBitmap As New Bitmap("Grapes.jpg")
' Get the color of a pixel within myBitmap.
Dim pixelColor As Color = myBitmap.GetPixel(50, 50)
' Fill a rectangle with pixelColor.
Dim pixelBrush As New SolidBrush(pixelColor)
e.Graphics.FillRectangle(pixelBrush, 0, 0, 100, 100)
End Sub
End
Class
All Replies
-
Monday, March 22, 2010 1:03 AM
I believe this is the code he is showing.Imports System.Drawing.Imaging Imports System.Runtime.InteropServices Public Class Form1 Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Public Function CopyDataToBitmap(ByVal data As Byte()) As Bitmap 'Here create the Bitmap to the know height, width and format Dim bmp As New Bitmap(640, 480, PixelFormat.Format24bppRgb) 'Create a BitmapData and Lock all pixels to be written Dim bmpData As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.[WriteOnly], bmp.PixelFormat) 'Copy the data from the byte array into BitmapData.Scan0 Marshal.Copy(data, 0, bmpData.Scan0, data.Length) 'Unlock the pixels bmp.UnlockBits(bmpData) 'Return the bitmap Return bmp End Function Dim I1 As Bitmap Dim Cx, Cy, TF, mp, o(256), grn(256), blue(256), red(256) As Integer Dim I4 As Bitmap Dim a As Integer = 0 Dim check As Boolean = False 'Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Dim I3 As Bitmap = New Bitmap(I1.Width, I1.Height) 'Dim I4 As Bitmap = New Bitmap(I1.Width, I1.Height) 'End Sub Private Function CropBitmap(ByRef bmp As Bitmap, ByVal cropX As Integer, ByVal cropY As Integer, ByVal cropWidth As Integer, ByVal cropHeight As Integer) As Bitmap Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight) Dim cropped As Bitmap = bmp.Clone(rect, bmp.PixelFormat) Return cropped End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ReadingFileDialog As New OpenFileDialog Dim fileOpened As Boolean = False If fileOpened = True Then PictureBox1.Image.Dispose() fileOpened = False End If ReadingFileDialog.Title = "Open My File" ' Restrict our choice of files to *.jpeg files. ReadingFileDialog.Filter = "Bitmap *.bmp|*.bmp|JPEG *.jpg|*.jpg|GIF *.gif|*.gif" If ReadingFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then 'ReadingFileDialog.ShowDialog() fileOpened = True Dim IPB As Bitmap = New Bitmap(ReadingFileDialog.FileName) If ReadingFileDialog.FileName <> "" Then PictureBox1.Image = New Bitmap(ReadingFileDialog.FileName) End If End If I1 = PictureBox1.Image End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim Cx, Cy, o(256), grn(256), blue(256), red(256) As Integer Dim o1(256), r1(256), b1(256), g1(256) As Integer 'For histogram & rect draw box Dim PHX(1240) Dim DFR(1280, 1024), DFG(1280, 1024), DFB(1280, 1024), PHV(1600, 1200), HCX(1024) As String Dim DFRT(1280, 1024), DFGT(1280, 1024), DFBT(1280, 1024) As Integer '### added temp color channels Dim CT As Color ' color object Dim wow As Boolean = False 'Dim LMP As Point = MousePosition Dim I3 As Bitmap '= New Bitmap(I1.Width, I1.Height)` ' PictureBox1.Image = CropBitmap(I4, PictureBox1.Left, PictureBox1.Top, I4.Width / 2, I4.Height / 2) 'Code for getting percentage of comparing Cy,Cx to I1 ' For Cx = 0 To I1.Width - 1 'For Cy = 0 To I1.Height - 1 'I1 = PictureBox1.Image 'CT = I1.GetPixel(20, 20) 'get the pixel data from the bitmap pixel 'PB2.Text = CInt(((Cy + 1) / I1.Height) * 100) & "%" 'DFR(20, 20) = CT.R 'DFR() is string (8-bit) 'PB2.Text = I1.Width 'DFG(Cx, Cy) = CT.G 'PB1.Text = DFG(Cx, Cy) 'DFB(Cx, Cy) = I1.Height 'PB1.Text = DFB(Cx, Cy) 'PB1.Text = (((CInt(Cx) + 1) / I1.Width) * 100) & "%" 'Next 'Next 'I3(0, 0) = I4 ' I3(0, 1) = New Bitmap(I4.Width, I4.Height, 400, Drawing.Imaging.PixelFormat.Format24bppRgb, DFR(20, 20)) 'PictureBox1.Image = I3(0, 0) 'PictureBox1.Height = I3(0, 0).Height 'PictureBox1.Width = I3(0, 0).Width ' Dim x, y As Integer ' 'Get color of the pixel ' For x = 1 To I1.Height 'For y = 1 To I1.Width 'Dim B(x, y) As Color 'A(x, y) = CT.R 'B(x, y) = CT 'B(x + 1, y + 1) = B(x, y) 'Next 'Next End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Dim d As New Drawing.Bitmap(1, 1) Dim b As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(d) b.CopyFromScreen(New Drawing.Point(MousePosition.X, MousePosition.Y), New Drawing.Point(0, 0), d.Size) Dim c As Drawing.Color = d.GetPixel(0, 0) PictureBox1.BackColor = c PB2.Text = PictureBox1.BackColor.Name End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click End Sub Public Sub GetPixel_Example(ByVal e As PaintEventArgs) ' Create a Bitmap object from an image file. Dim myBitmap As New Bitmap("Grapes.jpg") ' Get the color of a pixel within myBitmap. Dim pixelColor As Color = myBitmap.GetPixel(50, 50) ' Fill a rectangle with pixelColor. Dim pixelBrush As New SolidBrush(pixelColor) e.Graphics.FillRectangle(pixelBrush, 0, 0, 100, 100) End Sub End Class
Always Lost in Code, Always mark answers as correct if they answer you question and solve your problem. This way others when searching for similar problems can find the answer faster. -
Thursday, April 01, 2010 1:11 PM
Mathew,
Here is a link to a post that describes image effects. It doesn't give any code but gives a good explanation. A search came up with 600 posts about image effects. A little searching with various terms will get the information for you.
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/bb516bd5-d959-4558-855c-0154ba80ce1e/
Curtis
Always Lost in Code, Always mark answers as correct if they answer you question and solve your problem. This way others when searching for similar problems can find the answer faster.

