2011-01-19 100 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像素。