作为我的ongoing quest的一部分,为实验产生刺激,我遇到了一个奇怪的问题。图像的哪些属性可能会导致尺寸问题?
这次的预期结果是通过将图像分成相同大小的片段并随机交换这些片段来“混洗”图像。这在最初的测试中运行良好,我很快就忘记了这个程序。当我的同事用她的形象尝试时,细分突然变小了。
为了说明问题,我已经填充每个段矩形阴影线刷:
图像对A显示我的来自测试图像(来自Facebook下载)的初步结果。图像对B显示了应用于我的同事测试图像的相同操作;请注意,阴影区域之间有间隙。在我修改GIMP中的原始图像并重新保存之后,第三个图像对显示相同的效果。 (我原本是这样做的,看看方向是否有影响 - 事实并非如此)。
在我看来,从GIMP导出图像的过程影响图像的某些属性,从而导致尺寸被错误地解释。有什么方法可以检测并纠正?
我的代码(编辑为您的理智):
this.original = Image.FromFile(this.filename);
Image wholeImage = (Image)this.original.Clone();
int segwidth = (int)Math.Floor((double)(this.original.Width/segsX));
int segheight = (int)Math.Floor((double)(this.original.Height/segsY));
int segsCount = segsX * segsY;
Image[] segments = new Image[segsCount];
for (i = 0; i < segsCount; i++)
{
x = (i % segsX);
y = (int)Math.Floor((double)(i/segsX));
segments[i] = Crop(wholeImage, new Rectangle(x * segwidth, y * segheight, segwidth, segheight), (i%2>0));
}
// Call to an array shuffling helper class
using (Graphics g = Graphics.FromImage(wholeImage))
{
for (j = 0; j < segsCount; j++)
{
x = (j % segsX);
y = (int)Math.Floor((double)(j/segsX));
insertPoint = new Point(x * segwidth, y * segheight);
g.DrawImage(segments[j], insertPoint);
}
}
wholeImage.Save(this.targetfolder + Path.DirectorySeparatorChar + aggr_filename, ImageFormat.Png);
// The cropping function (including the hatch generation, which would be commented out when no longer needed)
static private Image Crop(Image wholeImage, Rectangle cropArea, Boolean odd = true)
{
Bitmap cropped = new Bitmap(cropArea.Width, cropArea.Height);
Rectangle rect = new Rectangle(0, 0, cropArea.Width, cropArea.Height);
System.Drawing.Drawing2D.HatchBrush brush;
if (odd)
{
brush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Plaid, Color.Red, Color.Blue);
}
else
{
brush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Plaid, Color.Beige, Color.CadetBlue);
}
using(Graphics g = Graphics.FromImage(cropped))
{
g.DrawImage(wholeImage, rect, cropArea, GraphicsUnit.Pixel);
g.FillRectangle(brush, rect);
}
return cropped as Image;
}
use using(this.original = Image.FromFile(this.filename)){.. 。}为图像中立场。这不会解决问题,但否则会导致内存泄漏。 – CaptainPlanet 2011-01-19 19:12:42
啊,谢谢CaptainPlanet!仍然不是C#本地... :) – 2011-01-19 19:21:21