i have a problem with cutting an image.
What i want to do is, to cut an image in some slices, mostly quadratic.
The code isn’t yet refined for boundary values and shouldn’t cut the whole image in an approriate size, but thats currently not the problem.
My problem is, that the first image which has been cut is really a part of the original image, but the 2nd image (or all after it) are only black. i am just at a dead end and i don’t know what the problem is.
var sourceImg = System.Drawing.Image.FromFile(args[numcount]);
int cutsizeHeight = sourceImg.Height < cutsize ? sourceImg.Height : cutsize;
int cutsizeWidht = sourceImg.Width < cutsize ? sourceImg.Width : cutsize;
int cutPassesHeight = (int)(sourceImg.Height / cutsize) == 0 ? 1 : (int)(sourceImg.Height / cutsize);
int cutPassesWidth = (int)(sourceImg.Width / cutsize) == 0 ? 1 : (int)(sourceImg.Width / cutsize);
for (int i = 0; i < cutPassesHeight; i++)
{
for (int j = 0; j < cutPassesWidth; j++)
{
var mem = new MemoryStream();
var sourcePositionX = i * cutsizeHeight;
var sourcePositionY = j * cutsizeWidht;
var cutRectangle = new System.Drawing.Rectangle(0, 0, cutsizeWidht, cutsizeHeight);
var newImage = new Bitmap(cutRectangle.Width, cutRectangle.Height);
Graphics g = Graphics.FromImage(newImage);
g.DrawImage(sourceImg, cutRectangle, sourcePositionX, sourcePositionY, cutsizeWidht, cutsizeHeight, GraphicsUnit.Pixel);
newImage.Save(mem, ImageFormat.Png);
var saveImage = System.Drawing.Image.FromStream(mem);
var fileName = System.IO.Path.GetFileName(args[numcount]);
var pureName = fileName.Split('.');
fileName = pureName[0] + i.ToString() + "_" + j.ToString() + ".png";
saveImage.Save(@"C:\usr\test\" + fileName);
}
}
This is my code snippet.
Currently i am testing it with an imagesize 5906 * 1773. The slice dimensions per cutted image should be 2096 * 1773. The code above ignores therefore the rest of 1714 in the width (2096 * 2 = 4192, rest of 5906 – 4192 = 1714). But thats okay, though.
The problem is, that the second one won’t be the image, but it is black.
Okay, the solution is: Use the original code but this was wrong, so instead of using:
it’s correct to use:
X at the coordinate system stands for width, i just made a small X/Y mistake here. now it works.