how to cut image in 9 pieces and control width and height? - c#

HI guys i have problem in this code i want to display my photo cut in 9 pieces and display it in 9 pictureboxs to make puzzle game wish anyone could help .
Thanks In Advance
var knight = new Image[9];
var H = Image.FromFile("1425435_630471227004342_2061223205_o.jpg");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
var index = i * 3 + j;
knight[index] = new Bitmap(200,200);
var m = Graphics.FromImage(knight[index]);
Rectangle r = new Rectangle( i * (knight[index].Width / 3),
j*(knight[index].Height / 3),
knight[index].Width / 3,
knight[index].Height / 3);
m.DrawImage(H, r, r, GraphicsUnit.Pixel);
m.Dispose();
}
}
pictureBox1.Image = knight[0];
pictureBox2.Image = knight[1];
pictureBox3.Image = knight[2];
pictureBox4.Image = knight[3];
pictureBox5.Image = knight[4];
pictureBox6.Image = knight[5];
pictureBox7.Image = knight[6];
pictureBox8.Image = knight[7];
pictureBox9.Image = knight[8];

Change this
Rectangle r = new Rectangle( i * (knight[index].Width / 3),
j*(knight[index].Height / 3),
knight[index].Width / 3,
knight[index].Height / 3);
m.DrawImage(H, r, r, GraphicsUnit.Pixel);
to this:
out of the loop:
// old size of the parts:
int ow = H.Width / 3;
int oh = H.Height / 3;
// new size of the parts:
int nw = knight[0].Width;
int nh = knight[0].Height;
// inner loop:
Rectangle rDest = new Rectangle(0, 0, nw, nh);
Rectangle rSource = new Rectangle(i * ow, j * oh, ow, oh);
m.DrawImage(H, rDest, rSource , GraphicsUnit.Pixel);
Note: If the proportions of the Original Image and the 9 PBs is different there will be a distortion:

You are not using a separate source rectangle (as TaW mentioned). Also you are not fully occupying the picturebox.
Try this code:
knight[index] = new Bitmap(640, 360); //enter the size of the original image
Rectangle src = new Rectangle(i * (knight[index].Width / 3), j * (knight[index].Height / 3), knight[index].Width / 3, knight[index].Height / 3);
Rectangle des = new Rectangle(0, 0, knight[index].Width, knight[index].Height);
m.DrawImage(H, des, src, GraphicsUnit.Pixel);

Related

A generic error occurred in GDI+ During a Bitmap Save

When I run this code
Bitmap im = new Bitmap(600, 600, PixelFormat.Format16bppGrayScale);
int height = im.Height;
int width = im.Width;
Point p1 = new Point(0, 0);
Point p2 = new Point(im.Size.Width, 0);
Point p3 = new Point(im.Width / 2, im.Height);
Random r = new Random();
Point p = new Point(r.Next(0, im.Size.Width), r.Next(0, im.Size.Height));
BitmapData data = im.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format16bppGrayScale);
const int stride = 2;
int wsrtide = data.Stride;
unsafe
{
IntPtr osc0 = data.Scan0;
ushort* sc0 = (ushort*)data.Scan0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
*sc0 = 0;
sc0 += 1;
}
}
for (long i = 0; i < width * height; i++)
{
if (i % 1_000_000 == 0)
{
Console.WriteLine(i);
}
var ran = r.Next(0, 3);
Point tp;
switch (ran)
{
case 0:
tp = new Point((p1.X + p.X) / 2, (p1.Y + p.Y) / 2);
sc0 = (ushort*)((int)osc0 + (wsrtide * tp.Y + (tp.X * stride)));
*sc0 = ushort.MaxValue;
p = tp;
break;
case 1:
tp = new Point((p2.X + p.X) / 2, (p2.Y + p.Y) / 2);
sc0 = (ushort*)((int)osc0 + (wsrtide * tp.Y + (tp.X * stride)));
*sc0 = ushort.MaxValue;
p = tp;
break;
case 2:
tp = new Point((p3.X + p.X) / 2, (p3.Y + p.Y) / 2);
sc0 = (ushort*)((int)osc0 + (wsrtide * tp.Y + (tp.X * stride)));
*sc0 = ushort.MaxValue;
p = tp;
break;
}
}
im.UnlockBits(data);
im.Save(Environment.CurrentDirectory + "\\img.png");
im.Dispose();
It throws where I save the bitmap
System.Runtime.InteropServices.ExternalException: A generic error
occurred in GDI+.`
I am almost certain that this isn't caused by file permissions, when I give the program admin permission and delete the image to reset permissions it still throws. I can confirm that this code swapped with BitmapSetPixel() on RGB works.
My suspicion is that I'm messing up the pointers, but I'm not really sure. Also, curiously enough, it makes empty png files even though it throws.
The purpose of this code is to generate a Sierpinski triangle using the chaos game method.
Your problem is the Format16bppGrayScale i don't think the GDI supports it very well.
Basically if you just create the Bitmap in Format16bppGrayScale, and save it with nothing else, it still gives the error.
I have taken the liberty to rewrite your method Format32bppPArgb
private unsafe static void Main(string[] args)
{
var height = 600;
var width = 600;
var p1 = new Point(0, 0);
var p2 = new Point(width, 0);
var p3 = new Point(width / 2, height);
var r = new Random();
var p = new Point(r.Next(0, width), r.Next(0, width));
using (var im = new Bitmap(width, height, PixelFormat.Format32bppPArgb))
{
var data = im.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);
var sc0 = (int*)data.Scan0;
var pLen = sc0 + height * width;
var black = Color.Black.ToArgb();
var white = Color.White.ToArgb();
for (var pI = sc0; pI < pLen; pI++)
*pI = black;
for (long i = 0; i < width * height; i++)
{
Point tp;
switch (r.Next(0, 3))
{
case 0:
tp = new Point((p1.X + p.X) / 2, (p1.Y + p.Y) / 2);
*(sc0 + tp.Y + tp.X * width) = white;
p = tp;
break;
case 1:
tp = new Point((p2.X + p.X) / 2, (p2.Y + p.Y) / 2);
*(sc0 + tp.Y + tp.X * width) = white;
p = tp;
break;
case 2:
tp = new Point((p3.X + p.X) / 2, (p3.Y + p.Y) / 2);
*(sc0 + tp.Y + tp.X * width) = white;
p = tp;
break;
}
}
im.UnlockBits(data);
im.Save(#"D:\img.png", ImageFormat.Png);
}
}
Result
You can convert it after the fact if you want, add pepper and salt to taste
Also if you get rid of all the points and cache the colors this will be a bit faster

Crop an image by curve split lines for puzzle in winforms

I'm trying to crop an image in windows forms (C#). I did it with straight corners. But I want to split it with curve lines.
A Single image is split into a 3x3 image and then placed in 9 different pictureBoxes. Here is my current code:
Image img = pictureBox1.Image;
int widththird = (int)((double)img.Width / 3.0 + 0.5);
int heightthird = (int)((double)img.Height / 3.0 + 0.5);
Bitmap[,] bmps = new Bitmap[3, 3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
bmps[i, j] = new Bitmap(widththird, heightthird);
Graphics g = Graphics.FromImage(bmps[i,j]);
g.DrawImage(img, new Rectangle(0, 0, widththird, heightthird), new
Rectangle(j * widththird, i * heightthird, widththird, heightthird),
GraphicsUnit.Pixel);
g.Dispose();
}
}
pictureBox2.Image = bmps[0, 0];
pictureBox3.Image = bmps[0, 1];
pictureBox4.Image = bmps[0, 2];
pictureBox5.Image = bmps[1, 0];
pictureBox6.Image = bmps[1, 1];
pictureBox7.Image = bmps[1, 2];
pictureBox8.Image = bmps[2, 0];
pictureBox9.Image = bmps[2, 1];
pictureBox10.Image = bmps[2, 2];
Q: How can I can crop/split the image with curves and export the pieces?
My output:
I want it cropped like this:

How to split a random image into 9 equal parts for puzzle game

I am trying to split an image into 9 equal parts. Like we see in a puzzle game. Image can be any random image. I am trying some code but it is not splitting it into equal parts - it needs coordinate values for rectangles, but I need simple general code to split an image into equal parts.
I've an image. I want to crop it or split into 9 equal parts. But the below code just crops the same part of the image from the right top corner every time.
var imgarray = new Image[9];
var img = Image.FromFile("media\\a.png");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
var index = i * 3 + j;
imgarray[index] = new Bitmap(104, 104);
var graphics = Graphics.FromImage(imgarray[index]);
graphics.DrawImage(img, new Rectangle(0, 0, 104, 104), new Rectangle(i * 104, j * 104, 104, 104), GraphicsUnit.Pixel);
graphics.Dispose();
}
}
Some other tidy ups...
var imgarray = new Image[9];
var img = Image.FromFile("media\\a.png");
for (int i = 0; i < 9; i++)
{
imgarray[i] = new Bitmap(104, 104);
var graphics = Graphics.FromImage(imgarray[i]);
graphics.DrawImage (
img,
new Rectangle(0, 0, 104, 104),
new Rectangle( (i%3) * 104, (i/3) * 104, 104, 104),
GraphicsUnit.Pixel);
graphics.Dispose();
}
}

How to create circle with bitmap c#

Hello How to draw a circle with the bitmap. That is, I need to get the image of the circle, using it to draw something.
Use ColorTranslator.FromHtml for this purpose.
This will give you the corresponding System.Drawing.Color:
using (Bitmap btm = new Bitmap(25, 30))
{
using (Graphics grf = Graphics.FromImage(btm))
{
using (Brush brsh = new SolidBrush(ColorTranslator.FromHtml("#ff00ffff")))
{
grf.FillEllipse(brsh, 0, 0, 19, 19);
}
}
}
Or Refer Code:
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(bmp);
Pen blackPen = new Pen(Color.Black);
int x = pictureBox1.Width/4;
int y = pictureBox1.Height/4;
int width = pictureBox1.Width / 2;
int height = pictureBox1.Height / 2;
int diameter = Math.Min(width, height);
g.DrawEllipse(blackPen, x, y, diameter, diameter);
pictureBox1.Image = bmp;
If the PictureBox already contains a bitmap, replace the first and second lines with:
Graphics g = Graphics.FromImage(pictureBox1.Image);
Referance Link:
http://www.c-sharpcorner.com/Forums/Thread/30986/
Hope Its Helpful.
Bitmap b = new Bitmap(261, 266);// height & width of picturebox
int xo = 50, yo = 50;// center of circle
double r, rr;
r = 20;
rr = Math.Pow(r, 2);
for (int i = xo - (int)r; i <= xo + r; i++)
for (int j = yo - (int)r; j <= yo + r; j++)
if (Math.Abs(Math.Pow(i - xo, 2) + Math.Pow(j - yo, 2) - rr) <= r)
b.SetPixel(i, j, Color.Black);
pictureBox1.Image = b;

Application.Run() throws Object is current in use exception C#

well i'm developing an eyetracker. So i manage to get my 2 cams going, but when i call the following method the program throws "Object is currently in use elsewhere" exception on Application.Run(...).
public void processImage(Bitmap bitmap)
{
Bitmap aq = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), PixelFormat.Format24bppRgb);
Invert a = new Invert();
aq = a.Apply(aq);
AForge.Imaging.Image.FormatImage(ref aq);
IFilter filter = Grayscale.CommonAlgorithms.BT709;
aq = filter.Apply(aq);
Threshold th = new Threshold(200);
aq = th.Apply(aq);
//find the biggest blob
BlobCounter bl = new BlobCounter(aq);
int i = bl.ObjectsCount;
Blob bigblob = null;
int areaant = 0;
Blob[] blobs = bl.GetObjects(aq, true);
for (int j = 0; j < i; j++)
{
if (blobs[j].Area == 1116)
{
j++;
if (j == i)
break;
}
if (blobs[j].Area > areaant)
{
areaant = blobs[j].Area;
bigblob = blobs[j];
}
}
Bitmap bitmap2 = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bitmap2);
int radius = 1;
int x = 0;
int y = 0;
int h = 25;
Pen redPen = new Pen(Color.Red, 2);
if (i > 1)
{
Blob fil2 = bigblob;
x = fil2.CenterOfGravity.X;
y = fil2.CenterOfGravity.Y;
g.DrawEllipse(redPen,
(int)(x - radius),
(int)(y - radius),
(int)(radius * 5),
(int)(radius * 5));
}
redPen.Dispose();
g.Dispose();
pictureBox1.Image = bitmap2;
pictureBox2.Image = aq;
}
So i imagine it has to do with the bitmap cloning method, because sometimes it throws the exception there.
Reading diferent problems of other people and their soultion, i've tried several things:
-Starting each cam with a diferent thread with the same form
-in other solution they suggested that the problem was that the two threads where accesing the bitmap. So i separate the forms, one for each camera (thread), and then i tried to use the method procesImage(..) in only one thread. Didn't work.
Any help would be much appreciated

Categories

Resources