图像的哪些属性可能会导致尺寸问题?

时间:2011-01-19 18:55:50

标签: c# graphics image-processing

作为我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;
}

2 个答案:

答案 0 :(得分:1)

- 根据OP的要求添加了答案 -

只是为了它,它把它移植到php并且它可以正常工作(边缘边缘,而不是单个瓷砖周围的边距。但是,这使我进入了绘制图像的msdn页面(图像,点) ,这似乎是基于显示设备的dpi而不是固定的像素尺寸来渲染分离的图像。你的裁剪功能使用rect并指定每像素的测量,但你的重绘例程使用不同的方法。尝试使用drawimage您在裁剪功能中用于图像重建的方法

答案 1 :(得分:0)

可能还有更多内容,但在图像大小不能被分段大小整除的情况下,我没有注意到处理额外像素的机制。您的测试用例是否可能完美契合?

假设100 x 100图像,10段:

100/10 = 10; 10 x 10 = 100像素。

假设100 x 100图像,13段:

100/13 = 7; 13 * 7 = 91像素。