作爲我的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