Pictureboxes有透明背景

时间:2014-06-17 08:48:18

标签: c# image transparency picturebox

我有两个图片盒:一个在背景中有一个图像(picturebox1)和另一个,(pic1)我想画一些东西(这个背景应该是透明的.--> pic1.BackColor = Color。透明的;)

看起来像那样:

除了字体外,一切都很好用。为什么它有黑色边框?

我的代码看起来像这样:

private void InBitmapZeichnen()
{
    Graphics g1 = Graphics.FromImage(bmp12);
    g1.PageUnit = GraphicsUnit.Pixel;
    //g1.InterpolationMode = InterpolationMode.HighQualityBilinear;

    Font f = new Font("Verdana", 8f);
    Font f1 = new Font("Verdana", 8f);
    Font f2 = new Font("Verdana", 10, System.Drawing.FontStyle.Bold);
    Brush b = new SolidBrush(Color.YellowGreen);
    Brush b1 = new SolidBrush(Color.YellowGreen);
    Pen PenRaster = new Pen(Color.Black, 0.1f);

    if (mnuRaster.Checked == true)
    {
        float j = Rohrdurchmesser / (float)(trk.Value + 2);
        //g1.SmoothingMode = SmoothingMode.HighSpeed;
        for (int i = pic1.Width / (trk.Value + 2); i <= pic1.Width - pic1.Width / (trk.Value + 2); i += pic1.Width / (trk.Value + 2))
        {
            PointF PRaster1 = new PointF(i, 0);
            PointF PRaster2 = new PointF(i, pic1.Bottom);
            PointF PRaster3 = new PointF(0, i+4);
            PointF PRaster4 = new PointF(pic1.Right, i+4);
            g1.DrawString((j).ToString("0") + " mm", f, b, new PointF(i + 5, 5));
            g1.DrawString((j).ToString("0") + " mm", f, b, new PointF(5, i + 5));
            g1.DrawLine(PenRaster, PRaster1, PRaster2);
            g1.DrawLine(PenRaster, PRaster3, PRaster4);
            j += Rohrdurchmesser / (float)(trk.Value + 2);
        }                   
    }
}

当我为背景颜色选择颜色时,它可以正常工作:

2 个答案:

答案 0 :(得分:0)

我认为在开始绘制图片框之前需要先清除背景颜色。

          Graphics g1 = Graphics.FromImage(bmp12);
          // add clear
          g1.Clear(BackColor);

所以在你的代码中尝试这个语句...... !!!

答案 1 :(得分:0)

如果要在透明背景上绘制文字,请尝试使用GraphicsPath

private void InBitmapZeichnen()
{
    Graphics g1 = Graphics.FromImage(bmp12);
    g1.PageUnit = GraphicsUnit.Pixel;
    g1.SmoothingMode = SmoothingMode.AntiAlias;
    //g1.InterpolationMode = InterpolationMode.HighQualityBilinear;

    Font f = new Font("Verdana", 8f);
    Font f1 = new Font("Verdana", 8f);
    Font f2 = new Font("Verdana", 10, System.Drawing.FontStyle.Bold);
    Brush b = new SolidBrush(Color.YellowGreen);
    Brush b1 = new SolidBrush(Color.YellowGreen);
    Pen PenRaster = new Pen(Color.Black, 0.1f);

    if (mnuRaster.Checked == true)
    {
        float j = Rohrdurchmesser / (float)(trk.Value + 2);
        //g1.SmoothingMode = SmoothingMode.HighSpeed;
        for (int i = pic1.Width / (trk.Value + 2); i <= pic1.Width - pic1.Width / (trk.Value + 2); i += pic1.Width / (trk.Value + 2))
        {
            PointF PRaster1 = new PointF(i, 0);
            PointF PRaster2 = new PointF(i, pic1.Bottom);
            PointF PRaster3 = new PointF(0, i + 4);
            PointF PRaster4 = new PointF(pic1.Right, i + 4);

            using (var path = new GraphicsPath())
            {
                path.AddString((j).ToString("0") + " mm", f.FontFamily, (int)f.Style, f.Size, new Point(i + 5, 5), null);
                path.AddString((j).ToString("0") + " mm", f.FontFamily, (int)f.Style, f.Size, new Point(5, i + 5), null);
                g1.FillPath(b, path);
            }

            //g1.DrawString((j).ToString("0") + " mm", f, b, new PointF(i + 5, 5));
            //g1.DrawString((j).ToString("0") + " mm", f, b, new PointF(5, i + 5));
            g1.DrawLine(PenRaster, PRaster1, PRaster2);
            g1.DrawLine(PenRaster, PRaster3, PRaster4);
            j += Rohrdurchmesser / (float)(trk.Value + 2);
        }
    }
}

正如您所看到的,而不是DrawString我使用FillPath