How to detect the upmost black pixel - c#

I have an application that creates an image from a text inputed by the user. In that image, I want to detect where is the beginning of the letters in the top so I can stamp my brand's logo and crop image accordingly.
This is what I tried so far, but can't get it to work...
Bitmap myBitmap = new Bitmap(Image.FromFile(#"C:\Users\me\Desktop\test_textimage.jpg"));
for (int j = 0; j < 2000; j++) // loops Y axis
{
for (int i = 0; i < 3500; i++) // loops X axis
{
System.Drawing.Color color = myBitmap.GetPixel(i, j);
if (color == System.Drawing.Color.Black)
{ // breakpoint here... it never hits...
}
}
}
I am 100% sure the image is black font into white background. Tested into photoshop.
Can anyone help? Thanks.

Related

Matrix of photos

So I have a code that injects an image into my project through resources Image foodWorld = Resources.orange and I want to make a matrix out of this photo, so it can look like this:
I have this code but I don't know how to draw the matrix. Also, I don't know if this is the right way to draw it or not:
this.Width = 400;
this.Height = 300;
Bitmap b = new Bitmap(this.Width, this.Height);
for(int i = 0; i < this.Height; i++)
{
for(int j = 0; j < this.Width; j ++)
{
//fill the matrix
}
}
I am not too familiar with WinForms, but in WPF, I'd do it this way:
var columns = 15;
var rows = 10;
var imageWidth = 32;
var imageHeight = 32;
var grid = new Grid();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
//Get the image in your project; I'm not sure how this is done in WinForms
var b = new Bitmap(imageWidth, imageHeight);
//Display it
var pictureBox = new PictureBox();
pictureBox.Image = b;
//Set the position
Grid.SetColumn(j, pictureBox);
Grid.SetRow(i, pictureBox);
//Insert into the "matrix"
grid.Children.Add(pictureBox);
}
}
For moving Pacman, repeat the above, but for only one image. Store a reference to the current position and when certain keys are pressed,
Animate it's margin until it appears to be in an adjacent cell (for instance, if each cell is 16 pixels wide and pacman should be in the center of any given cell, animate the right margin by 16 pixels to get it into the cell on the right and so forth).
Once it has moved to another cell, set the new row and column based on the direction in which it last moved.
If there is a fruit at the new position, get the fruit at that position and remove it from the Grid. You can get it by using myGrid.Children[currentRow * totalColumns + currentColumn] assuming currentRow and currentColumn are both zero-based.
Repeat for each cell it must move to.
This does mean the matrix will have a fixed size, but in WPF, there is a Viewbox, which is convenient for these types of scenarios. Also, set the z-index of pacman to be greater than the fruits so it's always on top.

GetPixel always return 0 on png image

When i read image with GetPixel() color value is always black. My image is a png image.
I tried convert the image to bitmap before, but don't had success.
I believe which the problem isn't my code, because whether I open png image in Paint and only save it. The code read image correctly.
I load image like bellow
myImage = new Bitmap(fileName);
I need read image here
private void LoadImageMap(Bitmap value){
for (int col = 0; col < value.Width; col++)
{
for (int row = 0; row < value.Height; row++)
{
var color = value.GetPixel(col, row);
var color is black, always.
Sample of the image...
PNG image there is transparency and when pixel is full transparency GetPixel() result with zero value to RGB colors.
Then my code needed of the one if to validate this case.
the solution was like bellow:
var color = value.GetPixel(x, y);
if(color.A.Equals(0))
{
color = Color.White;
}
PNG use ARGB colors where A represent the alpha channel and whether alpha is zero this pixel has full transparence.
Since you passed in the bitmap to your code, you need to actually use that variable when calling GetPixel. Also, ref keyword is not needed in this case.
private void LoadImageMap(Bitmap value)
{
for (int col = 0; col < value.Width; col++)
{
for (int row = 0; row < value.Height; row++)
{
var color = value.GetPixel(col, row);
}
}
}

How can I save the location of every single pixel of my image in c#?

I am writing a programme that I need to save the location of every single pixel in my bitmap image in an array and later on in need to for example randomly turn off 300 of black pixels randomly. However I am not sure how to do that. I have written the following code but of course it does not work. Can anyone please tell me the right way of doing that?
The locations of every pixel are constant (every pixel has exactly one x and one y coordinate) so the requirement of save the location of every single pixel is vague.
I guess what you try to do is: Turn 300 pixels in an image black, but save the previous color so you can restore single pixels?
You could try this:
class PixelHelper
{
public Point Coordinate;
public Color PixelColor;
}
PixelHelper[] pixelBackup = new PixelHelper[300];
Random r = new Random();
for (int i = 0; i < 300; i++)
{
int xRandom = r.Next(bmp.Width);
int yRandom = r.Next(bmp.Height);
Color c = bmp.GetPixel(xRandom, yRandom);
PixelHelper[i] = new PixelHelper() { Point = new Point(xRandom, yRandom), PixelColor = c };
}
After that the pixelBackup array contains 300 objects that contain a coordinate and the previous color.
EDIT: I guess from the comment that you want to turn 300 random black pixels white and then save the result as an image again?
Random r = new Random();
int n = 0;
while (n < 300)
{
int xRandom = r.Next(bmp.Width);
int yRandom = r.Next(bmp.Height);
if (bmp.GetPixel(xRandom, yRandom) == Color.Black)
{
bmp.SetPixel(xRandom, yRandom, Color.White);
n++;
}
}
bmp.Save(<filename>);
This turns 300 distinct pixels in your image from black to white. The while loop is used so I can increase n only if a black pixel is hit. If the random coordinate hits a white pixel, another pixel is picked.
Please note that this code loops forever in case there are less than 300 pixels in your image in total.
The following will open an image into memory, and then copy the pixel data into a 2d array. It then randomly converts 300 pixels in the 2d array to black. As an added bonus, it then saves the pixel data back into the bitmap object, and saves the file back to disk.
I edited the code to ensure 300 distinct pixels were selected.
int x = 0, y = 0;
///Get Data
Bitmap myBitmap = new Bitmap("mold.jpg");
Color[,] pixelData = new Color[myBitmap.Width, myBitmap.Height];
for (y = 0; y < myBitmap.Height; y++)
for (x = 0; x < myBitmap.Width; x++)
pixelData[x,y] = myBitmap.GetPixel(x, y);
///Randomly convert 3 pixels to black
Random rand = new Random();
List<Point> Used = new List<Point>();
for (int i = 0; i < 300; i++)
{
x = rand.Next(0, myBitmap.Width);
y = rand.Next(0, myBitmap.Height);
//Ensure we use 300 distinct pixels
while (Used.Contains(new Point(x,y)) || pixelData[x,y] != Color.Black)
{
x = rand.Next(0, myBitmap.Width);
y = rand.Next(0, myBitmap.Height);
}
Used.Add(new Point(x, y)); //Store the pixel we have used
pixelData[x, y] = Color.White;
}
///Save the new image
for (y = 0; y < myBitmap.Height; y++)
for (x = 0; x < myBitmap.Width; x++)
myBitmap.SetPixel(x, y, pixelData[x, y]);
myBitmap.Save("mold2.jpg");

How can I save the required pixel from an image and draw the image using that pixels

Hi all I have written a sample code to find out the black color pixels from an image, now I would like to save all those pixels to an array and would like to save that particular image with the save pixels can some one help me
Assume my image that I am getting the pixels is as follows
I will read all the black pixels and would like to save them, from that I would like to re-image the Fallout with transparent background. This is what I have written
for (int i = 0; i < b.Width; i++)
{
for (int j = 0; j < b.Height; j++)
{
Color pixelColor = b.GetPixel(i, j);
Response.Write("The color is " + pixelColor);
if (pixelColor.ToArgb() == Color.Black.ToArgb())
{
//Will get black color here
}
}
}
Can some one help me out for the remaining.
Create a second bitmap with the same size
Bitmap newBitmap = new Bitmap(oldBitmap.Width, oldBitmap.Height);
You can compare the colors directly
if (pixeColor == Color.Black) {
Inside the if { }
newBitmap.SetPixel(...);
That's it, but if you just wan't a transparent version, you can use Bitmap.MakeTransparent(Color);
PS: GetPixel() and SetPixel() are extremely slow, try using unsafe bitmap access

Getting Hue from every pixel in an Image

I am trying to get the Hue or 'H' of every pixel of an image, then changing the colour of the pixel that has a specific Hue. I am trying to change all pixels that have a hue >= 210 and <=260, which is the different hues of blue.
Here is my code:
// 'i' is the image
// 'b' is the bitmap of the image
float y;
for (int a = 0; a < i.Height; a++)
{
for (int c = 0; c < i.Width; c++)
{
y = b.GetPixel(c, a).GetHue();
if (y >= 210 && y <= 260)
{
b.SetPixel(c, a, Color.Black);
}
}
}
The only problem is that it doesn't pickup any blue colours, which are from 210 - 260. I am pretty sure that I am doing this correctly, but I must not be since it isnt working.
Please post here if you can solve this problem, Thanks!
Edit: I put a breakpoint on SetPixel, and it gets called many, many times so now I will check if I am saving the picture right.
Edit 2: I figured it out! I wasn't saving the picture.
It kind of worked for me. In the code below I have a Windows Form with PictureBox called imgViwer. Then clicking the a button I execute the code:
private void HueFilter()
{
float y;
Bitmap i = (Bitmap)imgViwer.Image;
for (int a = 0; a < i.Height; a++)
{
for (int c = 0; c < i.Width; c++)
{
y = i.GetPixel(c, a).GetHue();
if (y >= 210 && y <= 260)
{
i.SetPixel(c, a, Color.Black);
}
}
}
imgViwer.Image = i;
}
In your case you have an output image called b that should be assigned back to the PictureBox to the refresh.
Use, LockBits method, Luke!
Or, i think, it`s better be done with Graphics context.

Categories