winform window media player 解决加载媒体文件时短暂黑屏问题

在用window media player  控件播放视频或图片的时候总是会出现短暂黑屏问题

解决思路:在加载下一个视频或图片的时候,在window media player 控件上覆盖一个图片

代码如下:

前段代码:增加一个pictureBox4

后端代码:将图片控件和window media player 增加到一个panel 中

  public Form1()
  {

      InitializeComponent();

   roundPanel1.Controls.Add(pictureBox4);

   roundPanel1.Controls.Add(axWindowsMediaPlayer1);

}

再给window media player赋值的时候同时给图片控件赋值

    private void axWindowsMediaPlayer1_PlayStateChange_1(object sender, _WMPOCXEvents_PlayStateChangeEvent e)
    {
        if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
        {

        axWindowsMediaPlayer1.URL = AppDomain.CurrentDomain.BaseDirectory + @"newImg/" + imgFileNameList[playnum] + ".jpg";//图片循环
        pictureBox4.Image= Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + @"newImg/" + imgFileNameList[playnum] + ".jpg");
         }

}  

roundPanel1控件代码(也可以直接用原生的panel) :

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

namespace VideoPlayer
{
    public class RoundPanel : Panel
    {
        private float exBorderSize = 0f;
        [Category("EX属性")]
        [DefaultValue(typeof(float), "0")]
        public float EXBorderSize
        {
            get => exBorderSize;
            set { exBorderSize = value; Invalidate(); }
        }

        private float exBorderRadius = 25f;
        [Category("EX属性")]
        [DefaultValue(typeof(float), "25")]
        public float EXBorderRadius
        {
            get => exBorderRadius;
            set { exBorderRadius = value; Invalidate(); }
        }

        private Color exBorderColor = Color.Transparent;
        [Category("EX属性")]
        [DefaultValue(typeof(Color), "Transparent")]
        public Color EXBorderColor
        {
            get => exBorderColor;
            set { exBorderColor = value; Invalidate(); }
        }

        private Color exButtonColor = Color.Lime;
        [Category("EX属性")]
        [DefaultValue(typeof(Color), "Lime")]
        public Color EXButtonColor
        {
            get { return exButtonColor; }
            set { exButtonColor = value; Invalidate(); }
        }

        private string exText = "RoundButton";
        [Category("EX属性")]
        [DefaultValue(typeof(string), "RoundButton")]
        public string EXText
        {
            get { return exText; }
            set { exText = value; Invalidate(); }
        }

        private Color exTextColor = Color.Black;
        [Category("EX属性")]
        [DefaultValue(typeof(Color), "Black")]
        public Color EXTextColor
        {
            get { return exTextColor; }
            set { exTextColor = value; Invalidate(); }
        }

        private Font exTextFont = new Font("微软雅黑", 12f, FontStyle.Regular);
        [Category("EX属性")]
        public Font EXTextFont
        {
            get { return exTextFont; }
            set { exTextFont = value; Invalidate(); }
        }

        public RoundPanel()
        {
            //SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            //SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            //SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            //SetStyle(ControlStyles.ResizeRedraw, true);
            //SetStyle(ControlStyles.DoubleBuffer, true);
            //SetStyle(ControlStyles.UserPaint, true);

            //BackColor = Color.Transparent;
            //Cursor = Cursors.Hand;
            //DoubleBuffered = true;
            //Size = new Size(210, 70);
        }

        private RectangleF GetRectangleF(RectangleF rectangleF, float borderSize)
        {
            float x = rectangleF.X + borderSize;
            float y = rectangleF.Y + borderSize;
            float w = rectangleF.Width - borderSize;
            float h = rectangleF.Height - borderSize;

            return new RectangleF(x, y, w, h);
        }

        private GraphicsPath GetGraphicsPath(RectangleF rectangleF, float borderRadius)
        {
            GraphicsPath path = new GraphicsPath();
            borderRadius = (borderRadius + 1f) * 2f;

            RectangleF topLeft = new RectangleF(rectangleF.X, rectangleF.Y, borderRadius, borderRadius);
            RectangleF topRight = new RectangleF(rectangleF.Width - borderRadius - 1f, rectangleF.Y, borderRadius, borderRadius);
            RectangleF bottomRight = new RectangleF(rectangleF.Width - borderRadius - 1f, rectangleF.Height - borderRadius - 1f, borderRadius, borderRadius);
            RectangleF bottomLeft = new RectangleF(rectangleF.X, rectangleF.Height - borderRadius - 1f, borderRadius, borderRadius);

            path.AddArc(topLeft, 180f, 90f);
            path.AddArc(topRight, 270f, 90f);
            path.AddArc(bottomRight, 0, 90f);
            path.AddArc(bottomLeft, 90f, 90f);

            path.CloseFigure();

            return path;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            //Region
            RectangleF rectangleF_Region = GetRectangleF(ClientRectangle, 0f);
            GraphicsPath path_Region = GetGraphicsPath(rectangleF_Region, exBorderRadius);
            Region = new Region(path_Region);

            //Border
            RectangleF rectangleF_Border = GetRectangleF(rectangleF_Region, 1f);//比Region小一圈
            GraphicsPath path_Border = GetGraphicsPath(rectangleF_Border, exBorderRadius);
            SolidBrush solidBrush_Border = new SolidBrush(exBorderColor);
            e.Graphics.FillPath(solidBrush_Border, path_Border);

            //Main
            RectangleF rectangleF_Main = GetRectangleF(rectangleF_Border, exBorderSize);
            GraphicsPath path_Main = GetGraphicsPath(rectangleF_Main, exBorderRadius);
            SolidBrush solidBrush_Main = new SolidBrush(exButtonColor);
            e.Graphics.FillPath(solidBrush_Main, path_Main);

            //Text
            SolidBrush solidBrush_Text = new SolidBrush(exTextColor);
            StringFormat exTextFormat = new StringFormat();
            exTextFormat.Alignment = StringAlignment.Center;
            exTextFormat.LineAlignment = StringAlignment.Center;
            e.Graphics.DrawString(exText, exTextFont, solidBrush_Text, rectangleF_Main, exTextFormat);
        }


    }
}
 

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值