繁体   English   中英

透明背景的画框

[英]Pictureboxes with transparent background

我有两个图片框:一个在背景中,里面有一个图片(picturebox1),另一个,(pic1)是我要绘画的东西(此背景应该是透明的。-> pic1.BackColor = Color.Transparent;)

看起来像这样:

除字体外,其他所有功能都很好。 为什么有黑色边框?

我的代码如下所示:

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);
        }                   
    }
}

当我为底色选择一种颜色时,它可以正常工作:

我认为在开始在图片框上绘制之前,需要清除“背景色”。

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

因此,请在您的代码中尝试使用此语句... !!!

如果要在透明背景上绘制文本,请尝试使用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);
        }
    }
}

如您所见,我使用FillPath代替了DrawString

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM