private void PixelEditor_Paint(object sender, PaintEventArgs e) { if (DesignMode) { return; } Graphics g = e.Graphics; int c = ClientSize.Width / PixelSize; int r = ClientSize.Height / PixelSize; if (TgtMousePos.X < 0 || TgtMousePos.Y < 0) { return; } for (int x = 0; x < c; x++) { for (int y = 0; y < r; y++) { int sx = TgtMousePos.X + x; int sy = TgtMousePos.Y + y; if (sx > TgtBitmap.Width || sy > TgtBitmap.Height) { return; } Color col = TgtBitmap.GetPixel(sx, sy); using (SolidBrush b = new SolidBrush(col)) { g.FillRectangle(b, new Rectangle(x * PixelSize, y * PixelSize, PixelSize, PixelSize)); g.DrawRectangle(Pens.Black, new Rectangle(x * PixelSize, y * PixelSize, PixelSize, PixelSize)); } } } }
private void pictureBox1_Paint(object sender, PaintEventArgs e) { if (DesignMode) { return; } Graphics g = e.Graphics; if (canvasSizeChanged) { pictureBox1.Width = (grid.NumOfCellsX * cellSize); pictureBox1.Height = (grid.NumOfCellsY * cellSize); // Need to update picturebox.image width/height Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height); pictureBox1.Image = new Bitmap(bmp, pictureBox1.Width, pictureBox1.Height); TgtBitmap = (Bitmap)pictureBox1.Image; int width = (grid.NumOfCellsX * cellSize) + 60; int height = (grid.NumOfCellsY * cellSize) + 75; this.MinimumSize = new Size(width, height); this.Size = new Size(width, height); canvasSizeChanged = false; } int cols = grid.NumOfCellsX; int rows = grid.NumOfCellsY; for (int x = 0; x < cols; x++) { for (int y = 0; y < rows; y++) { if (x > pictureBox1.Image.Width || y > pictureBox1.Image.Height) { continue; } Color col = TgtBitmap.GetPixel(x, y); using (SolidBrush b = new SolidBrush(col)) using (Pen p = new Pen(GridColor)) { Rectangle rect = new Rectangle((x * cellSize) + toolStrip.Width, (y * cellSize) + menuStrip.Height, cellSize, cellSize); g.FillRectangle(b, rect); if (isDrawingGrid) { DrawCell(p, rect, g); } } } } }
private void PixelEditor_Paint(object sender, PaintEventArgs e) { if (DesignMode) { return; } Graphics g = e.Graphics; int cols = ClientSize.Width / PixelSize; int rows = ClientSize.Height / PixelSize; if (TgtMousePos.X < 0 || TgtMousePos.Y < 0) { return; } for (int x = 0; x < cols; x++) { for (int y = 0; y < rows; y++) { int sx = TgtMousePos.X + x; int sy = TgtMousePos.Y + y; if (sx > TgtBitmap.Width || sy > TgtBitmap.Height) { continue; } Color col = TgtBitmap.GetPixel(sx, sy); using (SolidBrush b = new SolidBrush(col)) using (Pen p = new Pen(GridColor)) { Rectangle rect = new Rectangle(x * PixelSize, y * PixelSize, PixelSize, PixelSize); g.FillRectangle(b, rect); g.DrawRectangle(p, rect); } } } }
// Enlarge factor = 1 private void SaveBmpAsSpritePNG() { dialog.Filter = "PNG (*.png)|*.png|JPEG (*.jpeg;*.jpeg;*.jpe;*.jfif)|*.jpeg|GIF (*.gif)|*.gif|All files(*.*)|*.*"; dialog.FilterIndex = 1; dialog.RestoreDirectory = true; int width = grid.NumOfCellsX; int height = grid.NumOfCellsY; int EnlargeFactor = 1; if (hasUserSaved == false) { try { if (dialog.ShowDialog() == DialogResult.OK) { isDrawingGrid = false; hasUserSaved = true; Invalidate(); // Fancy math that basically divided the thing up how much you want it enlarged by // In this case its getting increased by 10 times 16 px -> 160 px Bitmap bmp = new Bitmap(width * EnlargeFactor, height * EnlargeFactor); for (int x = 0; x < width * EnlargeFactor; x++) { for (int y = 0; y < height * EnlargeFactor; y++) { bmp.SetPixel(x, y, TgtBitmap.GetPixel(x / EnlargeFactor, y / EnlargeFactor)); } } //bmp is our new image, specifically for the sake of saving to disk. bmp.Save(dialog.FileName, ImageFormat.Png); isDrawingGrid = true; Invalidate(); } } catch (Exception) { MessageBox.Show("There was a problem saving the file." + " Check the file permissions."); } } else { Bitmap bmp = new Bitmap(width * EnlargeFactor, height * EnlargeFactor); for (int x = 0; x < width * EnlargeFactor; x++) { for (int y = 0; y < height * EnlargeFactor; y++) { bmp.SetPixel(x, y, TgtBitmap.GetPixel(x / EnlargeFactor, y / EnlargeFactor)); } } bmp.Save(dialog.FileName, ImageFormat.Png); isDrawingGrid = true; Invalidate(); } }