This is my GetPixel/SetPixel code for increasing the reds and purples in an image
This is my first attempt at lockbits:
The problem is that inside the nested loop, I do not understand how to convert the GetPixel/SetPixel code to Lockbits code. I have read tutorials, what I need is an example. What is the proper way to convert the nested loop code from GetPixel/SetPixel to Lockbits? Thank you very much
C#:
[COLOR=#00008b]private[/COLOR][COLOR=#00008b]void[/COLOR][COLOR=#000000] redsAndPurplesToolStripMenuItem_Click([/COLOR][COLOR=#00008b]object[/COLOR][COLOR=#000000] sender, [/COLOR][COLOR=#2b91af]EventArgs[/COLOR][COLOR=#000000] e)[/COLOR]
[COLOR=#000000]{[/COLOR][INDENT][COLOR=#808080]// Get bitmap from picturebox[/COLOR]
[COLOR=#2b91af]Bitmap[/COLOR][COLOR=#000000] bmpMain = ([/COLOR][COLOR=#2b91af]Bitmap[/COLOR][COLOR=#000000])pictureBoxMain.[/COLOR][COLOR=#2b91af]Image[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]Clone[/COLOR][COLOR=#000000]();[/COLOR]
[COLOR=#808080]// search through each pixel via x, y coordinates, examine and make changes. Dont let values exceed 255 or fall under 0. [/COLOR]
[COLOR=#00008b]for[/COLOR][COLOR=#000000] ([/COLOR][COLOR=#2b91af]int[/COLOR][COLOR=#000000] y = [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000]; y < bmpMain.[/COLOR][COLOR=#2b91af]Height[/COLOR][COLOR=#000000]; y++)[/COLOR]
[COLOR=#00008b] for[/COLOR][COLOR=#000000] ([/COLOR][COLOR=#2b91af]int[/COLOR][COLOR=#000000] x = [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000]; x < bmpMain.[/COLOR][COLOR=#2b91af]Width[/COLOR][COLOR=#000000]; x++)[/COLOR]
[COLOR=#000000] {[/COLOR]
[COLOR=#000000] bmpMain[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]GetPixel[/COLOR][COLOR=#000000](x, y);[/COLOR]
[COLOR=#2b91af] Color[/COLOR][COLOR=#000000] c = bmpMain.[/COLOR][COLOR=#2b91af]GetPixel[/COLOR][COLOR=#000000](x, y);[/COLOR]
[COLOR=#2b91af] int[/COLOR][COLOR=#000000] myRed = c.R, myGreen = c.G, myBlue = c.B;[/COLOR]
[COLOR=#000000] myGreen [/COLOR][COLOR=#000000]-= [/COLOR][COLOR=#800000]128[/COLOR][COLOR=#000000];[/COLOR]
[COLOR=#00008b] if[/COLOR][COLOR=#000000] (myGreen < [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000]) myGreen = [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
bmpMain.[/COLOR][COLOR=#2b91af]SetPixel[/COLOR][COLOR=#000000](x, y, [/COLOR][COLOR=#2b91af]Color[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]FromArgb[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]255[/COLOR][COLOR=#000000], myRed, myGreen, myBlue));[/COLOR]
[COLOR=#000000] }[/COLOR]
[COLOR=#808080]// assign the new bitmap to the picturebox[/COLOR]
[COLOR=#000000]pictureBoxMain[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]Image[/COLOR][COLOR=#000000] = ([/COLOR][COLOR=#2b91af]Bitmap[/COLOR][COLOR=#000000])bmpMain;[/COLOR]
[COLOR=#808080]// Save a copy to the HD for undo / redo.[/COLOR]
[COLOR=#00008b]string[/COLOR][COLOR=#000000] myString = [/COLOR][COLOR=#2b91af]Environment[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]GetEnvironmentVariable[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"temp"[/COLOR][COLOR=#000000], [/COLOR][COLOR=#2b91af]EnvironmentVariableTarget[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]Machine[/COLOR][COLOR=#000000]);[/COLOR]
[COLOR=#000000]pictureBoxMain[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]Image[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]Save[/COLOR][COLOR=#000000](myString + [/COLOR][COLOR=#800000]"\\ColorAppRedo.png"[/COLOR][COLOR=#000000], [/COLOR][COLOR=#2b91af]System[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]Drawing[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]Imaging[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]ImageFormat[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]Png[/COLOR][COLOR=#000000])[/COLOR][/INDENT]
[COLOR=#000000]}
[/COLOR]
This is my first attempt at lockbits:
C#:
[COLOR=#00008b]private[/COLOR][COLOR=#00008b]void[/COLOR][COLOR=#000000] redsAndPurplesToolStripMenuItem_Click([/COLOR][COLOR=#00008b]object[/COLOR][COLOR=#000000] sender, [/COLOR][COLOR=#2b91af]EventArgs[/COLOR][COLOR=#000000] e)[/COLOR]
[COLOR=#000000]{[/COLOR][INDENT][COLOR=#808080]
// Get bitmap from picturebox[/COLOR]
[COLOR=#2b91af]Bitmap[/COLOR][COLOR=#000000] bmpMain = ([/COLOR][COLOR=#2b91af]Bitmap[/COLOR][COLOR=#000000])pictureBoxMain.[/COLOR][COLOR=#2b91af]Image[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]Clone[/COLOR][COLOR=#000000]();[/COLOR]
[COLOR=#2b91af]Rectangle[/COLOR][COLOR=#000000] rect = [/COLOR][COLOR=#00008b]new[/COLOR][COLOR=#2b91af]Rectangle[/COLOR][COLOR=#000000]([/COLOR][COLOR=#2b91af]Point[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]Empty[/COLOR][COLOR=#000000], bmpMain.[/COLOR][COLOR=#2b91af]Size[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#2b91af]BitmapData[/COLOR][COLOR=#000000] bmpData = bmpMain.[/COLOR][COLOR=#2b91af]LockBits[/COLOR][COLOR=#000000](rect, [/COLOR][COLOR=#2b91af]ImageLockMode[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]ReadOnly[/COLOR][COLOR=#000000], bmpMain.[/COLOR][COLOR=#2b91af]PixelFormat[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]// search through each pixel via x, y coordinates, examine and make changes. Dont let values exceed 255 or fall under 0. [/COLOR]
[COLOR=#00008b]for[/COLOR][COLOR=#000000] ([/COLOR][COLOR=#2b91af]int[/COLOR][COLOR=#000000] y = [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000]; y < bmpMain.[/COLOR][COLOR=#2b91af]Height[/COLOR][COLOR=#000000]; y++)[/COLOR]
[COLOR=#00008b] for[/COLOR][COLOR=#000000] ([/COLOR][COLOR=#2b91af]int[/COLOR][COLOR=#000000] x = [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000]; x < bmpMain.[/COLOR][COLOR=#2b91af]Width[/COLOR][COLOR=#000000]; x++)[/COLOR]
[COLOR=#000000] {[/COLOR]
[COLOR=#000000] bmpMain[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]GetPixel[/COLOR][COLOR=#000000](x, y);[/COLOR]
[COLOR=#2b91af] Color[/COLOR][COLOR=#000000] c = [/COLOR][COLOR=#00008b]new[/COLOR][COLOR=#2b91af]Color[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#2b91af]int[/COLOR][COLOR=#000000] myRed = c.R, myGreen = c.G, myBlue = c.B;[/COLOR]
[COLOR=#000000] myGreen [/COLOR][COLOR=#000000]-= [/COLOR][COLOR=#800000]128[/COLOR][COLOR=#000000];[/COLOR]
[COLOR=#00008b] if[/COLOR][COLOR=#000000] (myGreen < [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000]) myGreen = [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
bmpMain.[/COLOR][COLOR=#2b91af]SetPixel[/COLOR][COLOR=#000000](x, y, [/COLOR][COLOR=#2b91af]Color[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]FromArgb[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]255[/COLOR][COLOR=#000000], myRed, myGreen, myBlue));[/COLOR]
[COLOR=#000000] }[/COLOR]
[COLOR=#000000]
bmpMain[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]UnlockBits[/COLOR][COLOR=#000000](bmpData);
[/COLOR][COLOR=#808080]
// assign the new bitmap to the picturebox
[/COLOR][COLOR=#000000]pictureBoxMain[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]Image[/COLOR][COLOR=#000000] = ([/COLOR][COLOR=#2b91af]Bitmap[/COLOR][COLOR=#000000])bmpMain;[/COLOR]
[COLOR=#808080]// Save a copy to the HD for undo / redo.[/COLOR]
[COLOR=#00008b]string[/COLOR][COLOR=#000000] myString = [/COLOR][COLOR=#2b91af]Environment[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]GetEnvironmentVariable[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"temp"[/COLOR][COLOR=#000000], [/COLOR][COLOR=#2b91af]EnvironmentVariableTarget[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]Machine[/COLOR][COLOR=#000000]);[/COLOR]
[COLOR=#000000]pictureBoxMain[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]Image[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]Save[/COLOR][COLOR=#000000](myString + [/COLOR][COLOR=#800000]"\\ColorAppRedo.png"[/COLOR][COLOR=#000000], [/COLOR][COLOR=#2b91af]System[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]Drawing[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]Imaging[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]ImageFormat[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2b91af]Png[/COLOR][COLOR=#000000]);[/COLOR][/INDENT]
[COLOR=#000000]} [/COLOR]
The problem is that inside the nested loop, I do not understand how to convert the GetPixel/SetPixel code to Lockbits code. I have read tutorials, what I need is an example. What is the proper way to convert the nested loop code from GetPixel/SetPixel to Lockbits? Thank you very much