DrawPolygon() erases the old polygon when drawing - c#

I set the points and when the points of the same color form a square, I draw a polygon. But when a new square is formed, the old one disappears.
can you tell me how to make sure that when drawing a new polygon, the old one does not disappear?
in the checkpoint() function, I check whether there is a square of points of the same color and return e coordinates for drawing.
public partial class Form1 : Form
{
private Class1 Class1 = new Class1();
private CellState currentPlayer = CellState.Red;
public const int SIZE = 11;
public const int Icon_Size = 30;
public Form1()
{
InitializeComponent();
}
//ставит точки
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
var p = new Point((int)Math.Round(1f * e.X / Icon_Size), (int)Math.Round(1f * e.Y / Icon_Size));
if (Class1[p] == CellState.Empty)
{
Class1.SetPoint(p, currentPlayer);
currentPlayer = Class1.Inverse(currentPlayer);
Invalidate();
}
}
//рисуем
private void OnPaint(object sender, PaintEventArgs e)
{
e.Graphics.ScaleTransform(Icon_Size, Icon_Size);
//рисуем сеточку
using (var pen = new Pen(Color.Gainsboro, 0.1f))
{
for (int x = 1; x < SIZE; x++)
e.Graphics.DrawLine(pen, x, 1, x, SIZE - 1);
for (int y = 1; y < SIZE; y++)
e.Graphics.DrawLine(pen, 1, y, SIZE - 1, y);
}
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//рисуем точки
using (var brush = new SolidBrush(Color.White))
for (int x = 1; x < Form1.SIZE; x++)
for (int y = 1; y < Form1.SIZE; y++)
{
var p = new Point(x, y);
var cell = Class1[p];
if (cell != CellState.Empty)
{
brush.Color = StateToColor(cell);
e.Graphics.FillEllipse(brush, x - 0.2f, y - 0.2f, 0.4f, 0.4f);
}
}
using (var PenP = new Pen(Color.Black, 0.1f))
using (var brush = new SolidBrush(Color.White))
{
Class1.CheckPoint();
int i = Class1.CheckPoint()[0];
int j = Class1.CheckPoint()[1];
int cp = Class1.CheckPoint()[2];
if (cp == 1)
{
PenP.Color = Color.Red;
brush.Color = Color.IndianRed;
Point[] a = { new Point(i, j), new Point(i + 1, j), new Point(i + 1, j + 1), new Point(i, j + 1) };
e.Graphics.FillPolygon(brush, a);
e.Graphics.DrawPolygon(PenP, a);
}
if (cp == 2)
{
PenP.Color = Color.Blue;
brush.Color = Color.RoyalBlue;
Point[] a = { new Point(i, j), new Point(i + 1, j), new Point(i + 1, j + 1), new Point(i, j + 1) };
e.Graphics.FillPolygon(brush, a);
e.Graphics.DrawPolygon(PenP, a);
}
}
}
//условие смены цвета под ход игрока
Color StateToColor(CellState state, byte alpha = 255)
{
var res = state == CellState.Blue ? Color.Blue : Color.Red;
return Color.FromArgb(alpha, res);
}
}

Related

System.IndexOutOfRangeException in my Nearest Point Calculation Program

I received System.IndexOutOfRangeException in the part of the SearchForTheNextDotsTopRight() method
if ((pointsArray[i].X == j) &&
((pointsArray[i].Y) <= p.Y))
I just simply don't know why the array is not functioning in this situation. Looking forward for help, thanks you a lot!
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
namespace Draw_Test
{
public partial class Form1 : Form
{
public static int sizeOfArray = 20;
int TopBorderY = 100;
int LeftBorderX = 100;
int BottomBorderY = 800;
int RightBorderX = 800;
Point Base = new Point(0, 0);
Point[] pointsArray = new Point[sizeOfArray];
public Form1()
{
InitializeComponent();
}
private void PointsArray(PaintEventArgs e)
{
var rand = new Random();
for (int i = 0; i < sizeOfArray; i++)
{
pointsArray[i].X = rand.Next(100, 800);
pointsArray[i].Y = rand.Next(100, 800);
CreateDots(pointsArray[i], e);
}
Point Nearest = SearchForTheFirstDots(pointsArray);
CreateLines(Base, Nearest, e);
Point Next = SearchForTheNextDotsTopRight(Nearest, pointsArray);
CreateLines(Nearest, Next, e);
}
private Point SearchForTheNextDotsTopRight(Point p, Point[] pointsArray)
{
Point PointNext = p;
for (int j = p.X; j <= RightBorderX; j++)
{
for (int i = 0; i <= sizeOfArray; i++)
{
if ((pointsArray[i].X == j) &&
((pointsArray[i].Y) <= p.Y))
{
PointNext = pointsArray[i];
goto endofLoop:
}
}
}
endofLoop:
return PointNext;
}
private double CalculateDistance(Point p1, Point p2)
{
double distance = Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2);
return distance;
}
private Point SearchForTheFirstDots(Point[] pointsArray)
{
Point Nearest = pointsArray[0];
for (int i = 0; i < sizeOfArray; i++)
{
if (CalculateDistance(Base, pointsArray[i]) <
CalculateDistance(Base, Nearest))
{
Nearest = pointsArray[i];
}
}
//pointList.Add(Nearest);
return Nearest;
}
private void CreateDots(Point p, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Blue, 3);
int width = 3;
int height = 3;
int pointXform = p.X - width / 2;
int pointYform = p.Y - height / 2;
Rectangle r = new Rectangle(pointXform, pointYform, width, height);
g.DrawEllipse(pen, r);
}
private void CreateLines(Point p1, Point p2, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Blue, 3);
g.DrawLine(pen, p1, p2);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Point p1 = new Point(25, 25);
Point p2 = new Point(100, 150);
PointsArray(e);
}
}
}
replace:
for (int i = 0; i <= sizeOfArray; i++) //<---------------
{
if ((pointsArray[i].X == j) &&
((pointsArray[i].Y) <= p.Y))
{
PointNext = pointsArray[i];
goto endofLoop:
}
}
by
for (int i = 0; i < sizeOfArray; i++) //<---------------
{
if ((pointsArray[i].X == j) &&
((pointsArray[i].Y) <= p.Y))
{
PointNext = pointsArray[i];
goto endofLoop:
}
}

Best way to speed up CropTransparentPixels method

I have just wrote this method to crop transparent pixels from images.
It seems to work ok but it is very slow because of GetPixel - any ideas on how to make the algorithm logic quicker?
I know I can change the GetPixel for faster (but unsafe) access code and I might do so, however I am after ways to avoid doing a full scan. I want advice on how to make the logic behind this algorithm quicker.
public Bitmap CropTransparentPixels(Bitmap originalBitmap)
{
// Find the min/max transparent pixels
Point min = new Point(int.MaxValue, int.MaxValue);
Point max = new Point(int.MinValue, int.MinValue);
for (int x = 0; x < originalBitmap.Width; ++x)
{
for (int y = 0; y < originalBitmap.Height; ++y)
{
Color pixelColor = originalBitmap.GetPixel(x, y);
if (pixelColor.A == 255)
{
if (x < min.X) min.X = x;
if (y < min.Y) min.Y = y;
if (x > max.X) max.X = x;
if (y > max.Y) max.Y = y;
}
}
}
// Create a new bitmap from the crop rectangle
Rectangle cropRectangle = new Rectangle(min.X, min.Y, max.X - min.X, max.Y - min.Y);
Bitmap newBitmap = new Bitmap(cropRectangle.Width, cropRectangle.Height);
using (Graphics g = Graphics.FromImage(newBitmap))
{
g.DrawImage(originalBitmap, 0, 0, cropRectangle, GraphicsUnit.Pixel);
}
return newBitmap;
}
This is the method I ended up writing and it is much faster.
public static Bitmap CropTransparentPixels(this Bitmap bmp)
{
BitmapData bmData = null;
try
{
bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
int scanline = bmData.Stride;
IntPtr Scan0 = bmData.Scan0;
Point top = new Point(), left = new Point(), right = new Point(), bottom = new Point();
bool complete = false;
unsafe
{
byte* p = (byte*)(void*)Scan0;
for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
if (p[3] != 0)
{
top = new Point(x, y);
complete = true;
break;
}
p += 4;
}
if (complete)
break;
}
p = (byte*)(void*)Scan0;
complete = false;
for (int y = bmp.Height - 1; y >= 0; y--)
{
for (int x = 0; x < bmp.Width; x++)
{
if (p[x * 4 + y * scanline + 3] != 0)
{
bottom = new Point(x + 1, y + 1);
complete = true;
break;
}
}
if (complete)
break;
}
p = (byte*)(void*)Scan0;
complete = false;
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
if (p[x * 4 + y * scanline + 3] != 0)
{
left = new Point(x, y);
complete = true;
break;
}
}
if (complete)
break;
}
p = (byte*)(void*)Scan0;
complete = false;
for (int x = bmp.Width - 1; x >= 0; x--)
{
for (int y = 0; y < bmp.Height; y++)
{
if (p[x * 4 + y * scanline + 3] != 0)
{
right = new Point(x + 1, y + 1);
complete = true;
break;
}
}
if (complete)
break;
}
}
bmp.UnlockBits(bmData);
System.Drawing.Rectangle rectangle = new Rectangle(left.X, top.Y, right.X - left.X, bottom.Y - top.Y);
Bitmap b = new Bitmap(rectangle.Width, rectangle.Height);
Graphics g = Graphics.FromImage(b);
g.DrawImage(bmp, 0, 0, rectangle, GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch
{
try
{
bmp.UnlockBits(bmData);
}
catch { }
return null;
}
}

How to create control that draws a table in panel

I want create a control that draws a table in panel . My code is:
public class PanelZ : System.Windows.Forms.Panel
{
public static void Draw()
{
Panel p = new Panel();
p.Width = 200;
p.Height = 200;
Graphics g = p.CreateGraphics();
Pen mypen = new Pen(Brushes.Black, 1);
Font myfont = new Font("tahoma", 10);
int lines = 9;
float x = 0;
float y = 0;
float xSpace = p.Width / lines;
float yspace = p.Height / lines;
for (int i = 0; i < lines + 1; i++)
{
g.DrawLine(mypen, x, y, x, p.Height);
x += xSpace;
}
x = 0f;
for (int i = 0; i < lines + 1; i++)
{
g.DrawLine(mypen, x, y, p.Width, y);
y += yspace;
}
}
..but it dosen't draw a table; so what should I do?
This will work. But the numbers ought to be properties, as should the pen and then some.. Also: Properties ought to start with an uppercase letter.
public class PanelZ : System.Windows.Forms.Panel
{
public PanelZ() // a constructor
{
Width = 200;
Height = 200;
DoubleBuffered = true;
lines = 9;
}
public int lines { get; set; } // a property
protected override void OnPaint(PaintEventArgs e) // the paint event
{
base.OnPaint(e);
Graphics g = e.Graphics;
Pen mypen = new Pen(Brushes.Black, 1);
Font myfont = new Font("tahoma", 10);
float x = 0;
float y = 0;
float xSpace = Width / lines;
float yspace = Height / lines;
for (int i = 0; i < lines + 1; i++)
{
g.DrawLine(mypen, x, y, x, Height);
x += xSpace;
}
for (int i = 0; i < lines + 1; i++)
{
g.DrawLine(mypen, 0, y, Width, y);
y += yspace;
}
}
}
At work in VS:
Note that this only colors pixels. There is no useful grid there, just pixels with color.. So, if you actually want to use the Font you define you will have to calculate the coodordinates and the bounding boxes.

C# Zoom In and Zoom Out On A Image

I want to make a program have zoom in and zoom out function.(2x,4x,8x)But I can't use a available zoom function.I have to write a new one.I have done importing bitmap image.And I can get rgb color for each pixel.I created matrisses colorR,colorG and colorB for r,g and b colors.After that I thought I can create the 2x zoomed image with SolidBrush on a panel.I will draw 2x image like this:
Original Image (For example 3x3 pixels) (p=pixel color and "_" for space)
p1_p2_p3
p4_p5_p6
p7_p8_p9
2x Zoomed Image (6x6 pixels because of orginal image size) (p=pixel color of orginal image and "_" for space)
p1_p1_p2_p2_p3_p3
p1_p1_p2_p2_p3_p3
p4_p4_p5_p5_p6_p6
p4_p4_p5_p5_p6_p6
p7_p7_p8_p8_p9_p9
p7_p7_p8_p8_p9_p9
I wrote one loop but it didn't work because it is complety wrong.So how can I write for loops ?
private void button4_Click(object sender, EventArgs e) {
listBox1.Items.Clear();//insignificant
listBox2.Items.Clear();//insignificant
listBox3.Items.Clear();//insignificant
using (OpenFileDialog dlg = new OpenFileDialog()) {
dlg.Title = "Open Image";
dlg.Filter = "*.bmp|*.bmp|*.*|*.*";
if (dlg.ShowDialog() == DialogResult.OK) {
pictureBox1.Image = new Bitmap(dlg.FileName);
}
}
}
private void button1_Click(object sender, EventArgs e) {
Graphics my2xImage = panel1.CreateGraphics();
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
int[,] colorR = new int[bmpHeight, bmpWidth];
int[,] colorG = new int[bmpHeight, bmpWidth];
int[,] colorB = new int[bmpHeight, bmpWidth];
for (int y = 0; y < bmpHeight; y++) {
for (int x = 0; x < bmpWidth; x++) {
Color pixelColor = bmpFirst.GetPixel(x, y);
colorR[x, y] = pixelColor.R;
colorG[x, y] = pixelColor.G;
colorB[x, y] = pixelColor.B;
listBox1.Items.Add("(" + (x + 1) + "," + (y + 1) + ")" + " " + colorR[x, y]);//insignificant
listBox2.Items.Add("(" + (x + 1) + "," + (y + 1) + ")" + " " + colorG[x, y]);//insignificant
listBox3.Items.Add("(" + (x + 1) + "," + (y + 1) + ")" + " " + colorB[x, y]);//insignificant
}
}
//for (int y = 0; y < (bmpHeight * 2); y++)
//{
// for (int x = 0; x < (bmpWidth * 2); x++)
// {
// Color mySpecialColor = Color.FromArgb(colorR[x,y], colorG[x,y], colorB[x,y]);
// SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
// my2xImage.FillRectangle(pixelBrush, x, y, 1, 1);
// }
//}
}
private void button5_Click(object sender, EventArgs e) {
}
private void button2_Click(object sender, EventArgs e) {
}
This is insane, but if you really must do it this way, then try something like this:
int dx = x*2;
int dy = y*2;
colorR[dx ,dy ] = pixelColor.R;
colorR[dx+1,dy ] = pixelColor.R;
colorR[dx ,dy+1] = pixelColor.R;
colorR[dx+1,dy+1] = pixelColor.R;
colorG[dx ,dy ] = pixelColor.G;
colorG[dx+1,dy ] = pixelColor.G;
colorG[dx ,dy+1] = pixelColor.G;
colorG[dx+1,dy+1] = pixelColor.G;
colorB[dx ,dy ] = pixelColor.B;
colorB[dx+1,dy ] = pixelColor.B;
colorB[dx ,dy+1] = pixelColor.B;
colorB[dx+1,dy+1] = pixelColor.B;
You should use the DrawImage method of the Graphics class.
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = Graphics.FromImage(bmpFirst);
// Draw image to screen.
g.DrawImage(newImage, destRect, x, y, width, height, units);
Look here: https://msdn.microsoft.com/en-us/library/ms142045(v=vs.110).aspx
Look here also: https://msdn.microsoft.com/en-us/library/k0fsyd4e(v=vs.110).aspx
You can even set the interpolation mode: https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode(v=vs.110).aspx
You are looking for the NearestNeighbor interpolation mode.
He is the solution.
private void button4_Click(object sender, EventArgs e )
{
listBox1.Items.Clear();
listBox2.Items.Clear();
listBox3.Items.Clear();
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "*.bmp|*.bmp|*.*|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(dlg.FileName);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = panel1.CreateGraphics();
int[,] colorR = new int[bmpHeight*2 , bmpWidth*2];
int[,] colorG = new int[bmpHeight*2 , bmpWidth*2];
int[,] colorB = new int[bmpHeight*2 , bmpWidth*2];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
Color pixelColor = bmpFirst.GetPixel(x, y);
int dx = x * 2;
int dy = y * 2;
colorR[dx, dy] = pixelColor.R;
colorR[dx + 1, dy] = pixelColor.R;
colorR[dx, dy + 1] = pixelColor.R;
colorR[dx + 1, dy + 1] = pixelColor.R;
colorG[dx, dy] = pixelColor.G;
colorG[dx + 1, dy] = pixelColor.G;
colorG[dx, dy + 1] = pixelColor.G;
colorG[dx + 1, dy + 1] = pixelColor.G;
colorB[dx, dy] = pixelColor.B;
colorB[dx + 1, dy] = pixelColor.B;
colorB[dx, dy + 1] = pixelColor.B;
colorB[dx + 1, dy + 1] = pixelColor.B;
}
}
for (int y = 0; y < (bmpHeight*2); y++)
{
for (int x = 0; x < (bmpWidth*2); x++)
{
Color mySpecialColor = Color.FromArgb(colorR[x, y], colorG[x, y], colorB[x, y]);
SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
g.FillRectangle(pixelBrush, x, y, 1, 1);
}
}
You can use for loops for color[dx,dy] parts.Here it is for 8x zoom.
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = panel3.CreateGraphics();
int[,] colorR = new int[bmpHeight * 8, bmpWidth * 8];
int[,] colorG = new int[bmpHeight * 8, bmpWidth * 8];
int[,] colorB = new int[bmpHeight * 8, bmpWidth * 8];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
Color pixelColor = bmpFirst.GetPixel(x, y);
int dx = x * 8;
int dy = y * 8;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
colorR[dx + j, dy + i] = pixelColor.R;
colorG[dx + j, dy + i] = pixelColor.G;
colorB[dx + j, dy + i] = pixelColor.B;
}
}
}
Full code (Also you can download project: Link
private void button4_Click(object sender, EventArgs e )
{
tabControl1.SelectedTab = tabPage1;
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "*.bmp|*.bmp|*.jpg|*.jpg|*.*|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(dlg.FileName);
}
}
if (pictureBox1.Image == null)
{
MessageBox.Show("Please choose a image file.");
}
else
{
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
if (bmpHeight > 100 || bmpWidth > 100)
{
MessageBox.Show("Image size can't be bigger than 100x100 pixels");
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button10.Enabled = false;
}
else
{
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button10.Enabled = false;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
tabControl1.SelectedTab = tabPage2;
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = panel1.CreateGraphics();
int[,] colorR = new int[bmpWidth * 2 , bmpHeight *2];
int[,] colorG = new int[bmpWidth * 2 , bmpHeight *2];
int[,] colorB = new int[bmpWidth * 2 , bmpHeight *2];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
Color pixelColor = bmpFirst.GetPixel(x, y);
int dx = x * 2;
int dy = y * 2;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
colorR[dx + i, dy + j] = pixelColor.R;
colorG[dx + i, dy + j] = pixelColor.G;
colorB[dx + i, dy + j] = pixelColor.B;
}
}
}
}
for (int y = 0; y < (bmpHeight*2); y++)
{
for (int x = 0; x < (bmpWidth*2); x++)
{
Color mySpecialColor = Color.FromArgb(colorR[x, y], colorG[x, y], colorB[x, y]);
SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
g.FillRectangle(pixelBrush, x, y, 1, 1);
}
}
}
private void button5_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
tabControl1.SelectedTab = tabPage3;
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = panel2.CreateGraphics();
int[,] colorR = new int[bmpWidth * 4, bmpHeight * 4];
int[,] colorG = new int[bmpWidth * 4, bmpHeight * 4];
int[,] colorB = new int[bmpWidth * 4, bmpHeight * 4];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
Color pixelColor = bmpFirst.GetPixel(x, y);
int dx = x * 4;
int dy = y * 4;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
colorR[dx + i, dy + j] = pixelColor.R;
colorG[dx + i, dy + j] = pixelColor.G;
colorB[dx + i, dy + j] = pixelColor.B;
}
}
}
}
for (int y = 0; y < (bmpHeight * 4); y++)
{
for (int x = 0; x < (bmpWidth * 4); x++)
{
Color mySpecialColor = Color.FromArgb(colorR[x, y], colorG[x, y], colorB[x, y]);
SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
g.FillRectangle(pixelBrush, x, y, 1, 1);
}
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void button5_Click_1(object sender, EventArgs e)
{
}
private void tabPage2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
tabControl1.SelectedTab = tabPage4;
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = panel3.CreateGraphics();
int[,] colorR = new int[bmpWidth * 8, bmpHeight * 8];
int[,] colorG = new int[bmpWidth * 8, bmpHeight * 8];
int[,] colorB = new int[bmpWidth * 8, bmpHeight * 8];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
Color pixelColor = bmpFirst.GetPixel(x, y);
int dx = x * 8;
int dy = y * 8;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
colorR[dx + i, dy + j] = pixelColor.R;
colorG[dx + i, dy + j] = pixelColor.G;
colorB[dx + i, dy + j] = pixelColor.B;
}
}
}
}
for (int y = 0; y < (bmpHeight * 8); y++)
{
for (int x = 0; x < (bmpWidth * 8); x++)
{
Color mySpecialColor = Color.FromArgb(colorR[x, y], colorG[x, y], colorB[x, y]);
SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
g.FillRectangle(pixelBrush, x, y, 1, 1);
}
}
}
private void button5_Click_2(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
tabControl1.SelectedTab = tabPage5;
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = panel4.CreateGraphics();
int[,] colorR = new int[bmpWidth, bmpHeight];
int[,] colorG = new int[bmpWidth, bmpHeight];
int[,] colorB = new int[bmpWidth, bmpHeight];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
Color pixelColor = bmpFirst.GetPixel(x, y);
colorR[x, y] = pixelColor.R;
colorG[x, y] = pixelColor.G;
colorB[x, y] = pixelColor.B;
}
}
int[,] colorSR = new int[bmpWidth /2, bmpHeight /2];
int[,] colorSG = new int[bmpWidth /2, bmpHeight /2];
int[,] colorSB = new int[bmpWidth /2, bmpHeight /2];
for (int i = 0; i < bmpWidth / 2; i++)
{
for (int j = 0; j < bmpHeight /2; j++)
{
colorSR[i, j] = (colorR[2 * i, 2 * j] + colorR[2 * i, 2 * j + 1] + colorR[2 * i + 1, 2 * j] + colorR[2 * i + 1, 2 * j + 1]) / 4;
colorSG[i, j] = (colorG[2 * i, 2 * j] + colorG[2 * i, 2 * j + 1] + colorG[2 * i + 1, 2 * j] + colorG[2 * i + 1, 2 * j + 1]) / 4;
colorSB[i, j] = (colorB[2 * i, 2 * j] + colorB[2 * i, 2 * j + 1] + colorB[2 * i + 1, 2 * j] + colorB[2 * i + 1, 2 * j + 1]) / 4;
}
}
for (int y = 0; y < (bmpHeight / 2); y++)
{
for (int x = 0; x < (bmpWidth / 2); x++)
{
Color mySpecialColor = Color.FromArgb(colorSR[x, y], colorSG[x, y], colorSB[x, y]);
SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
g.FillRectangle(pixelBrush, x, y, 1, 1);
}
}
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void button6_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
tabControl1.SelectedTab = tabPage6;
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = panel5.CreateGraphics();
int[,] colorR = new int[bmpWidth, bmpHeight];
int[,] colorG = new int[bmpWidth, bmpHeight];
int[,] colorB = new int[bmpWidth, bmpHeight];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
Color pixelColor = bmpFirst.GetPixel(x, y);
colorR[x, y] = pixelColor.R;
colorG[x, y] = pixelColor.G;
colorB[x, y] = pixelColor.B;
}
}
int[,] colorSR = new int[bmpWidth / 4, bmpHeight / 4];
int[,] colorSG = new int[bmpWidth / 4, bmpHeight / 4];
int[,] colorSB = new int[bmpWidth / 4, bmpHeight / 4];
for (int i = 0; i < bmpWidth / 4; i++)
{
for (int j = 0; j < bmpHeight / 4; j++)
{
colorSR[i, j] = (colorR[4 * i, 4 * j] + colorR[4 * i, 4 * j + 1] + colorR[4 * i + 1, 4 * j] + colorR[4 * i + 1, 4 * j + 1]) / 4;
colorSG[i, j] = (colorG[4 * i, 4 * j] + colorG[4 * i, 4 * j + 1] + colorG[4 * i + 1, 4 * j] + colorG[4 * i + 1, 4 * j + 1]) / 4;
colorSB[i, j] = (colorB[4 * i, 4 * j] + colorB[4 * i, 4 * j + 1] + colorB[4 * i + 1, 4 * j] + colorB[4 * i + 1, 4 * j + 1]) / 4;
}
}
for (int y = 0; y < (bmpHeight / 4); y++)
{
for (int x = 0; x < (bmpWidth / 4); x++)
{
Color mySpecialColor = Color.FromArgb(colorSR[x, y], colorSG[x, y], colorSB[x, y]);
SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
g.FillRectangle(pixelBrush, x, y, 1, 1);
}
}
}
private void button7_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
tabControl1.SelectedTab = tabPage7;
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = panel6.CreateGraphics();
int[,] colorR = new int[bmpWidth, bmpHeight];
int[,] colorG = new int[bmpWidth, bmpHeight];
int[,] colorB = new int[bmpWidth, bmpHeight];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
Color pixelColor = bmpFirst.GetPixel(x, y);
colorR[x, y] = pixelColor.R;
colorG[x, y] = pixelColor.G;
colorB[x, y] = pixelColor.B;
}
}
int[,] colorSR = new int[bmpWidth / 8, bmpHeight / 8];
int[,] colorSG = new int[bmpWidth / 8, bmpHeight / 8];
int[,] colorSB = new int[bmpWidth / 8, bmpHeight / 8];
for (int i = 0; i < bmpWidth / 8; i++)
{
for (int j = 0; j < bmpHeight / 8; j++)
{
colorSR[i, j] = (colorR[8 * i, 8 * j] + colorR[8 * i, 8 * j + 1] + colorR[8 * i + 1, 8 * j] + colorR[8 * i + 1, 8 * j + 1]) / 4;
colorSG[i, j] = (colorG[8 * i, 8 * j] + colorG[8 * i, 8 * j + 1] + colorG[8 * i + 1, 8 * j] + colorG[8 * i + 1, 8 * j + 1]) / 4;
colorSB[i, j] = (colorB[8 * i, 8 * j] + colorB[8 * i, 8 * j + 1] + colorB[8 * i + 1, 8 * j] + colorB[8 * i + 1, 8 * j + 1]) / 4;
}
}
for (int y = 0; y < (bmpHeight / 8); y++)
{
for (int x = 0; x < (bmpWidth / 8); x++)
{
Color mySpecialColor = Color.FromArgb(colorSR[x, y], colorSG[x, y], colorSB[x, y]);
SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
g.FillRectangle(pixelBrush, x, y, 1, 1);
}
}
}
private void button8_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "*.bmp|*.bmp|*.jpg|*.jpg|*.*|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(dlg.FileName);
}
}
if (pictureBox1.Image == null)
{
MessageBox.Show("Please choose a image file.");
}
else
{
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
if (bmpHeight > 800 || bmpWidth > 800)
{
MessageBox.Show("Image size can't be bigger than 800x800 pixels.");
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button10.Enabled = false;
}
else
{
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button5.Enabled = true;
button6.Enabled = true;
button7.Enabled = true;
button10.Enabled = false;
}
}
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
private void button10_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
tabControl1.SelectedTab = tabPage8;
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = panel7.CreateGraphics();
int[,] colorR = new int[bmpWidth, bmpHeight];
int[,] colorG = new int[bmpWidth, bmpHeight];
int[,] colorB = new int[bmpWidth, bmpHeight];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
Color pixelColor = bmpFirst.GetPixel(x, y);
colorR[x, y] = pixelColor.R;
colorG[x, y] = pixelColor.G;
colorB[x, y] = pixelColor.B;
}
}
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
int dx = bmpWidth - 1;
int dy = bmpHeight - 1;
Color mySpecialColor = Color.FromArgb(colorR[dx - x, dy - y], colorG[dx - x, dy - y], colorB[dx - x, dy - y]);
SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
g.FillRectangle(pixelBrush, x, y, 1, 1);
}
}
}
private void button9_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "*.bmp|*.bmp|*.jpg|*.jpg|*.*|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(dlg.FileName);
}
}
if (pictureBox1.Image == null)
{
MessageBox.Show("Please choose a image file.");
}
else
{
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
if (bmpHeight > 800 || bmpWidth > 800)
{
MessageBox.Show("Image size can't be bigger than 800x800 pixels");
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button10.Enabled = false;
}
else
{
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button10.Enabled = true;
}
}
}
}

Image splitting into 9 pieces

My Code:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
ngr.Dispose();
List<Image> list = new List<Image>();
Graphics g = Graphics.FromImage(pictureBox1.Image);
Brush redBrush = new SolidBrush(Color.Red);
Pen pen = new Pen(redBrush,3);
MessageBox.Show(pictureBox1.Image.Width + " " + pictureBox1.Image.Height);
for (int i = 0; i < pictureBox1.Image.Width; i = (pictureBox1.Image.Width / 3) + i)
{
for (int y = 0; y < pictureBox1.Image.Height; y = (pictureBox1.Image.Height / 3) + y)
{
Rectangle r = new Rectangle(i, y, pictureBox1.Image.Width / 3, pictureBox1.Image.Height / 3);
g.DrawRectangle(pen,r );
if (i > 0 && y > 0)
{
if (i + r.Width < pictureBox1.Image.Width && y + r.Height < pictureBox1.Image.Height)
{
list.Add(cropImage(pictureBox1.Image, r));
}
}
}
}
g.Dispose();
pictureBox1.Invalidate();
pictureBox1.Image = list[0];
}
private static Image cropImage(Image img, Rectangle cropArea)
{
Bitmap bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea, System.Drawing.Imaging.PixelFormat.DontCare);
return (Image)(bmpCrop);
}
This Code adds only 2 pieces to the list but not the other 7 pieces.
Please Help!!
Change this part of code and try again:
for (int i = 0; i < 3; i++)
{
for (int y = 0; y < 3; y++)
{
Rectangle r = new Rectangle(i*(pictureBox1.Image.Width / 3),
y*(pictureBox1.Image.Height / 3),
pictureBox1.Image.Width / 3,
pictureBox1.Image.Height / 3);
g.DrawRectangle(pen,r );
list.Add(cropImage(pictureBox1.Image, r));
}
}

Categories

Resources