一个GDI画文字的问题

zsrui 2016-08-30 04:27:17
我想做一个支持弹幕的视频播放器,用GDI来实时画出弹幕内容。可是我尝试画出的弹幕好像是被视频播放器给挡住了,看不见,这个怎么处理,请高手指点一下,谢谢!

弹幕内容是"我是画出来的文字!!!"

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace dmshipin
{
public partial class Form1 : Form
{
int i = 1500;
public Form1()
{
InitializeComponent();

}

private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = @"d:\h1.mp4";
//axWindowsMediaPlayer1.uiMode = "none";

}

private void timer1_Tick(object sender, EventArgs e)
{
System.Drawing.Size size = this.Size;
System.Drawing.Point point = this.Location;

int left = this.Left;
int width = this.Width;

if (i < 10)
{
i = width - 10;
}
else {
i = i - 5;
}
label1.Location = new Point(i, 40);
label1.BackColor = Color.Transparent;
label2.Location = new Point(i+10, 80);
label3.Location = new Point(i-5, 120);
label4.Location = new Point(i+20, 160);

////画出的文字
Graphics g = this.CreateGraphics();
Font font = new Font("华为宋体", 20);
//Point一样,只是值是浮点类型
PointF point1 = new PointF(150, 150);
g.DrawString("我是画出来的文字!!!", font, Brushes.Coral, point1);

}

}
}
...全文
253 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zsrui 2016-08-31
  • 打赏
  • 举报
回复
引用 9 楼 u012948520 的回复:
双缓存有个限制是graphBuffer的大小在allocte的时候确定,所以每次窗体大小发生改变都要重新allocate一次 有没有其他解决方案我不清楚,也是初学者,最近用到GDI画图
用了下面的代码,文字画不出来了。label能看到,但“我是画出来的文字”不显示了。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace dmshipin
{
    public partial class Form1 : Form
    {
        //GDI画图的双缓存设定
        private BufferedGraphics graphBuffer = null;


        int danmuweizhi_x = 1500;//默认弹幕起始位置
        public Form1()
        {
            InitializeComponent();
            
        }

        private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
        {
            
            axWindowsMediaPlayer1.currentPlaylist = axWindowsMediaPlayer1.newPlaylist("播放列表", "");
            //遍历打开的集合
            string[] bofangliebiao = new string[] { @"d:\h3.mp4", @"d:\h2.mp4", @"d:\h1.mp4" };
            foreach (string fn in bofangliebiao)
            {
                //添加播放列表
                axWindowsMediaPlayer1.currentPlaylist.appendItem(axWindowsMediaPlayer1.newMedia(fn));
            }

            //GDI画图的双缓存设定
            graphBuffer = (new BufferedGraphicsContext()).Allocate(axWindowsMediaPlayer1.CreateGraphics(), axWindowsMediaPlayer1.DisplayRectangle);
            
            //循环播放
            axWindowsMediaPlayer1.settings.setMode("loop", true);
            //播放
            axWindowsMediaPlayer1.Ctlcontrols.play();

            

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            System.Drawing.Size size = this.Size;
            System.Drawing.Point point = this.Location;

            int left = this.Left;
            int width = this.Width;

            if (danmuweizhi_x < -400)
            {
                danmuweizhi_x = width - 10;
            }
            else {
                danmuweizhi_x = danmuweizhi_x - 1;
            }
            label1.Location = new Point(danmuweizhi_x, 40);
            label2.Location = new Point(danmuweizhi_x + 10, 80);
            label3.Location = new Point(danmuweizhi_x - 5, 120);
            label4.Location = new Point(danmuweizhi_x + 20, 160);

            ////画出的文字
            //Graphics danmu = axWindowsMediaPlayer1.CreateGraphics();

            //GDI画图的双缓存设定
            Graphics danmu = graphBuffer.Graphics;
            Font font = new Font("黑体", 24);
            //Point一样,只是值是浮点类型
            PointF point1 = new PointF(danmuweizhi_x + 10, 250);
            SolidBrush grayBrush = new SolidBrush(Color.Gray);
            danmu.DrawString("我是画出来的文字!!!", font, Brushes.White, point1);
        }

    }
}
白衣如花 2016-08-31
  • 打赏
  • 举报
回复
双缓存有个限制是graphBuffer的大小在allocte的时候确定,所以每次窗体大小发生改变都要重新allocate一次 有没有其他解决方案我不清楚,也是初学者,最近用到GDI画图
白衣如花 2016-08-31
  • 打赏
  • 举报
回复
第二问不知道原因,第一问可以用双缓存
public partial class Form1 : Form
    {
        private BufferedGraphics graphBuffer = null;

        private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.URL = @"d:\h1.mp4";
            graphBuffer = (new BufferedGraphicsContext()).Allocate(axWindowsMediaPlayer1.CreateGraphics(), axWindowsMediaPlayer1.DisplayRectangle);

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Graphics g = graphBuffer.Graphics;
            ...
        }
    }
zsrui 2016-08-30
  • 打赏
  • 举报
回复
现在的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace dmshipin
{
    public partial class Form1 : Form
    {
        int i = 1500;
        public Form1()
        {
            InitializeComponent();
            
        }

        private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.URL = @"d:\h1.mp4";
            //axWindowsMediaPlayer1.uiMode = "none";

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            System.Drawing.Size size = this.Size;
            System.Drawing.Point point = this.Location;

            int left = this.Left;
            int width = this.Width;

            if (i < 10)
            {
                i = width - 10;
            }
            else {
                i = i - 1;
            }
            label1.Location = new Point(i, 40);
            label1.BackColor = Color.Transparent;
            label2.Location = new Point(i+10, 80);
            label3.Location = new Point(i-5, 120);
            label4.Location = new Point(i+20, 160);

            ////画出的文字
            Graphics g = this.CreateGraphics();
            Graphics danmu = axWindowsMediaPlayer1.CreateGraphics();
            Font font = new Font("黑体", 20);
            //Point一样,只是值是浮点类型
            PointF point1 = new PointF(i+10, 250);
            SolidBrush grayBrush = new SolidBrush(Color.Gray);
            danmu.DrawString("我是画出来的文字!!!", font, Brushes.White, point1);
        }

    }
}
zsrui 2016-08-30
  • 打赏
  • 举报
回复
引用 5 楼 u012948520 的回复:
g是this create的。所以drawString当然也是在窗体背景上啊

试试用播放器那个控件creategraphics


好用了,多谢多谢!!!
还有两个问题:
1、画出来的文字闪烁的很厉害,严重影响观看效果,我已经把timer的时间调整的很快了,但还是闪烁
2、当我全屏播放视频内容的时候,画出的文字和label的文字全都不显示了,这个是咋回事捏?

白衣如花 2016-08-30
  • 打赏
  • 举报
回复
g是this create的。所以drawString当然也是在窗体背景上啊 试试用播放器那个控件creategraphics
Poopaye 2016-08-30
  • 打赏
  • 举报
回复
引用 3 楼 shingoscar 的回复:
你需要拖个别的控件,将parent设定为axWindowsMediaPlayer1,将背景色设定为transparent
然后在拖出来的控件上绘制文字
Poopaye 2016-08-30
  • 打赏
  • 举报
回复
你需要拖个别的控件,将parent设定为axWindowsMediaPlayer1,将背景色设定为transparent
zsrui 2016-08-30
  • 打赏
  • 举报
回复
引用 1 楼 zsrui 的回复:
补充一下,现在label1、2、3、4的内容能看到,这个弹幕文字有背景,不喜欢。
Graphics g = this.CreateGraphics();画出的文字看不到,而且经过测试,它其实已经画出来了,但是被视频播放控件给挡住了。
zsrui 2016-08-30
  • 打赏
  • 举报
回复
补充一下,现在label1、2、3、4的内容能看到,这个弹幕文字有背景,不喜欢。 Graphics g = this.CreateGraphics();画出的文字看不到,而且经过测试,它其实已经画出来了,但是被视频播放控件给挡住了。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧