2011-01-19 99 views
0

作爲我的ongoing quest的一部分,爲實驗產生刺激,我遇到了一個奇怪的問題。圖像的哪些屬性可能會導致尺寸問題?

這次的預期結果是通過將圖像分成相同大小的片段並隨機交換這些片段來「混洗」圖像。這在最初的測試中運行良好,我很快就忘記了這個程序。當我的同事用她的形象嘗試時,細分突然變小了。

爲了說明問題,我已經填充每個段矩形陰影線刷:

A series of experimental images

圖像對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; 
} 
+0

use using(this.original = Image.FromFile(this.filename)){.. 。}爲圖像中立場。這不會解決問題,但否則會導致內存泄漏。 – CaptainPlanet 2011-01-19 19:12:42

+0

啊,謝謝CaptainPlanet!仍然不是C#本地... :) – 2011-01-19 19:21:21

回答

1

--added爲對的OP--

要求只爲它赫克答案,我移植這PHP和它的作品,你會想到就在身邊(頁邊距,圍繞個別瓷磚沒有利潤。然而,這讓我進入了msdn頁面的drawimage(圖像,點),它似乎基於顯示設備的dpi來呈現精確的圖像,而不是固定的像素尺寸。裁剪函數使用rect並指定每像素,但是您的重繪例程使用了不同的方法。請嘗試使用裁剪函數中使用的drawimage方法進行圖像重構

0

可能有更多的它,但我沒有注意到的機制與額外的像素處理的情況下當圖像尺寸不能被分段大小整除。你的測試用例是否可以完美匹配?

假設一個100×100的圖像,10個段:

100/10的

地板= 10; 10 x 10 = 100像素。

假設一個100×100的圖像,13個段:

一十三分之百= 7的

地板; 13 * 7 = 91像素。