【问题标题】:Pictureboxes with transparent background具有透明背景的图片框
【发布时间】:2014-08-07 04:58:21
【问题描述】:

我有两个图片框:一个在背景中,里面有一个图像(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);
        }                   
    }
}

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

【问题讨论】:

标签: c# image transparency picturebox


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2012-08-21
      • 2023-03-08
      • 1970-01-01
      • 2011-09-24
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      • 2013-08-16
      相关资源
      最近更新 更多