如何使用drawImage()

时间:2015-06-09 04:10:51

标签: c#

我在使用此功能将图像分成多个部分并将其移动一点时遇到问题。

问题在于,结果中矩形的高度不同于" node_height"变量

这是一张测试图片:measure.jpg 结果:measured.jpg 在这张图片中我使用了" node_height = 100"。它本来应该削减所有圈子。

代码:

    private void button2_Click(object sender, EventArgs e)
    {
        bmp = new Bitmap(source.Width, source.Height);
        bmp.SetResolution(source.HorizontalResolution, source.VerticalResolution);

        int node_height = trackBar1.Value;
        int shift = trackBar2.Value;
        int image_width = bmp.Width;
        int image_height = bmp.Height;
        double division = image_height / node_height;
        int nodes = Convert.ToInt32(division);

        Graphics g = Graphics.FromImage(bmp);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.None;
        g.PageScale = 1;
        g.PageUnit = GraphicsUnit.Pixel;

        g.Clear(Color.Transparent);

        for(var i = 0; i < nodes; i++) {
            int new_shift = RandomNumber(0,shift);

            int x = 0;
            int y = node_height * i;
            int w = image_width;
            int h = node_height;

            Rectangle source_rect = new Rectangle(x, y, w, h);
            Rectangle dest_rect = new Rectangle(new_shift, y, w, h);

            g.DrawImage(source, dest_rect, source_rect, GraphicsUnit.Pixel);
        }
        MessageBox.Show("Done!");
        pictureBox1.Image = bmp;
    }

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。我在随机序列中获得相同的数字。以下是我案Problem in generating random number

中解决方案的链接