简体   繁体   中英

PictureBox.CreateGraphics not saving

I have a PictureBox called map and I draw graphics onto it

map.CreateGraphics().FillRectangle(Brushes.Black, x+xx, y+yy, 1, 1);

It works absolutely fine, it draws the pixels, but when I attempt to save

map.Image.Save(@"C:\Users\Skye\Pictures\!Access\Map\map" + DateTime.Now.ToString("yyMMddHmmss") + ".png");

Edit: I have tried various saving methods ImageFormat.Bmp/ImageFormat.Png/etc

It brings up an error

An unhandled exception of type 'System.NullReferenceException' occurred in Terrain Genorator.exe
Additional information: Object reference not set to an instance of an object.

In Autos it says

map.Image       null
  1. How can I save pixels?

or

  1. How can I save pixels drawn on a PictureBox?

Found the solution with thanks to @valter

Make in Bitmap, make picturebox show the image, save either bitmap or picturebox.

Bitmap mapImage;
mapImage = new Bitmap(pWidth, pHeight);

mapImage.SetPixel(x + xx, y + yy, Color.Black);
map.Image = mapImage;

map.Image.Save(@"C:\Users\Skye\Pictures\!Access\Map\" + DateTime.Now.ToString("yyMMddHmmss") + ".png");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM