Reputation: 175
I'm new to c#, and I need to read the values for the pixels from a text file and then create an image file with the defined pixels. I found this code, but not sure how to use it:(
Please help! I don't have to draw anything on the screen, but in a jpg/bmp/png... file.
private void SetPixel_Example(PaintEventArgs e)
{
// Create a Bitmap object from a file.
Bitmap myBitmap = new Bitmap("Grapes.jpg");
// Draw myBitmap to the screen.
e.Graphics.DrawImage(myBitmap, 0, 0, myBitmap.Width,
myBitmap.Height);
// Set each pixel in myBitmap to black.
for (int Xcount = 0; Xcount < myBitmap.Width; Xcount++)
{
for (int Ycount = 0; Ycount < myBitmap.Height; Ycount++)
{
myBitmap.SetPixel(Xcount, Ycount, Color.Black);
}
}
// Draw myBitmap to the screen again.
e.Graphics.DrawImage(myBitmap, myBitmap.Width, 0,
myBitmap.Width, myBitmap.Height);
}
Upvotes: 0
Views: 524
Reputation: 61382
It looks like the only thing missing is Bitmap.Save
, which saves the result to a file. Details on MSDN.
myBitmap.Save(@"C:\example.png");
Upvotes: 1