1

我在pictureBox中绘制了一张图片,现在我想将它保存在文件夹中。我尝试了很多方法都没有奏效。我正在使用休闲代码绘制图像。我正在根据文本框值绘制图像。

 private void btnTransferBottleRegenerate_Click(object sender, EventArgs e)
    {

        float[] volumetransfer = new float[1];
        volumetransfer[0] = float.Parse(txtTransferVolume.Text);

        int[] percentages = new int[6];
        percentages[0] = int.Parse(txtTransferNotIdentified.Text);
        percentages[1] = int.Parse(txtTransferWaterBasedMud.Text);
        percentages[2] = int.Parse(txtTransferOilBasedMud.Text);
        percentages[3] = int.Parse(txtTransferWater.Text);
        percentages[4] = int.Parse(txtTransferHydrocarbonLiq.Text);
        percentages[5] = int.Parse(txtTransferGas.Text);

        Color[] colors = new Color[6];
        colors[0] = Color.Gray;
        colors[1] = Color.Chocolate; 
        colors[2] = Color.SaddleBrown;
        colors[3] = Color.Blue;
        colors[4] = Color.Red;
        colors[5] = Color.Lime;

        // Finally, call the method 
        DrawPercentages(percentages, colors, volumetransfer);

        //string filename = Application.StartupPath + "\\volumetransfer.jpg";

       // pictureBox1.Image.Save(Application.StartupPath + "\\Image\\picture1.jpg");
      //  pictureBox1.Refresh();
      //  if (pictureBox1 != null)
       // {
            pictureBox1.Image.Save(Application.StartupPath + "\\test.bmp");
       // }
    }

    private void DrawPercentages(int[] percentages, Color[] colors, float[] volumetransfer)
    {
        // Create a Graphics object to draw on the picturebox
        Graphics G = pictureBox1.CreateGraphics();

        // Calculate the number of pixels per 1 percent
        float pixelsPerPercent = pictureBox1.Height / volumetransfer[0];

        // Keep track of the height at which to start drawing (starting from the bottom going up)
        int drawHeight = pictureBox1.Height;

        // Loop through all percentages and draw a rectangle for each
        for (int i = 0; i < percentages.Length; i++)
        {
            // Create a brush with the current color
            SolidBrush brush = new SolidBrush(colors[i]);
            // Update the height at which the next rectangle is drawn.
            drawHeight -= (int)(pixelsPerPercent * percentages[i]);
            // Draw a filled rectangle
            G.FillRectangle(brush, 0, drawHeight, pictureBox1.Width, pixelsPerPercent * percentages[i]);
        }
    }

}

}

当我单击“重新生成”按钮时,它将在 pictureBox 中绘制图像,然后我想将其保存在文件夹中。我有这样的设计。

4

2 回答 2

1

一个解决方案是在位图上绘制,将其设置为图像,PictureBox然后保存:

private void DrawPercentages(int[] percentages, Color[] colors, float[] volumetransfer){
   Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
   using(Graphics G = Graphics.FromImage(bmp)){
       //...
   }

   pictureBox1.Image = bmp;
}

然后你的代码应该可以完美运行,没有任何问题。

于 2013-02-07T17:49:59.717 回答
0

首先,您应该在正确的事件 PictureBox1_Paint中进行绘制,以便绘制的图像保持可见更好:重新绘制),即使您的窗口得到例如:调整大小。

之后,您可以使用@Hans Passant 发布的片段 -如何保存图形对象将绘制的图像保存到磁盘

// global to be accesible within paint
float[] volumetransfer = new float[1];
int[] percentages = new int[6];
Color[] colors = new Color[6];

private void btnTransferBottleRegenerate_Click(object sender, EventArgs e)
{            
    /// initialization goes here

    // force pictureBox to be redrawn
    // so resizing your window won't let your rectangles disapear
    pictureBox1.Invalidate();

    using (var bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height))
    {
        pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
        bmp.Save(@"e:\temp\test.png"); //Application.StartupPath + "\\Image\\picture1.jpg"
    }
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    // use GraphicsObject of PaintEventArgs
    Graphics G = e.Graphics;

    float pixelsPerPercent = pictureBox1.Height / volumetransfer[0];
    int drawHeight = pictureBox1.Height;

    for (int i = 0; i < percentages.Length; i++)
    {
        SolidBrush brush = new SolidBrush(colors[i]);
        drawHeight -= (int)(pixelsPerPercent * percentages[i]);
        G.FillRectangle(brush, 0, drawHeight, pictureBox1.Width, pixelsPerPercent * percentages[i]);
    }           
}

另一方面,您DrawPercentage(..)可以返回一个新图像- 之后您可以将其分配给图片框并将其保存为pictureBox1.Image.Save(...)

private void button1_Click(object sender, EventArgs e)
{
    float[] volumetransfer = new float[1];
    int[] percentages = new int[6];
    Color[] colors = new Color[6];

    /// initialization goes here

    pictureBox1.Image = CreateImage(volumetransfer, percentages, colors);
    pictureBox1.Image.Save(@"e:\temp\test.png");
}

private Image CreateImage(float[] volumetransfer, int[] percentages, Color[] colors)
{
    Image img = new Bitmap(pictureBox1.Width, pictureBox1.Height);
    Graphics g = Graphics.FromImage(img);

    float pixelsPerPercent = pictureBox1.Height / volumetransfer[0];
    int drawHeight = pictureBox1.Height;

    for (int i = 0; i < percentages.Length; i++)
    {
        SolidBrush brush = new SolidBrush(colors[i]);
        drawHeight -= (int)(pixelsPerPercent * percentages[i]);
        g.FillRectangle(brush, 0, drawHeight, pictureBox1.Width, pixelsPerPercent * percentages[i]);
    }

    return img;
}
于 2013-02-07T17:51:36.107 回答