Click here to Skip to main content
14,172,361 members
Click here to Skip to main content

Stats

756K views
30.8K downloads
291 bookmarked
Posted 14 Sep 2010
Licenced CPOL

PVS.AVPlayer - Audio and Video Player Library

, 21 May 2019
Microsoft Media Foundation (MF) based easy-to-use library with many added features
Code Project PVS 0.95 Article
Image PVS-95-1.png
Image PVS-95-2.png
Image PVS-95-3.png
Image PVS-95-4.png
Image PVS-95-5.png
Image PVS-95-6.png
Image PVS-95-7.gif
PVS.AVPlayer
PVS.AVPlayer .NET 2.0
PVS.AVPlayer.XML
PVS.AVPlayer .NET 3.0
PVS.AVPlayer.XML
PVS.AVPlayer .NET 3.5
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.0
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.5
PVS.AVPlayer .NET 4.5.1
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.5.2
PVS.AVPlayer.XML
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.6
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.6.1
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.6.2
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.7
PVS.AVPlayer .NET 4.7.1
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.7.2
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer Licenses
PVS.AVPlayer All Source Code
AVPlayerExample
AVPlayerExample
AVPlayerExample.csproj.user
bin
Debug
PVS.AVPlayer.XML
Release
Dialogs
Display Overlays
obj
Debug
Release
x86
Debug
Release
Properties
Resources
Crystal Italic1.ttf
WingDings3a.ttf
Voice Recorder
FolderView
FolderView
bin
Debug
PVS.AVPlayer.XML
Release
FolderView.csproj.user
obj
Release
x86
Debug
Release
Properties
Resources
Crystal Italic1.ttf
PVS.AVPlayer
AVPlayerExample.csproj.user
PVS.AVPlayer.dll
PVS.AVPlayer.XML
Custom Items
Native Methods
Bob.png
Crystal Italic1.ttf
Dial Green.png
Dial Normal 2.png
Dial Red 2.png
Dial Red.png
Kaizen-4.png
Media Normal.ico
Media Paused.ico
Media Paused1.ico
Media Playing.ico
PVSLogo.png
PVSLogoOutline.png
VU Meter.png
WingDings3a.ttf
Various
PVS.AVPlayer Licenses
About Dialog
PVS.AVPlayer.dll
PVS.AVPlayer.XML
Custom Items
FolderView.csproj.user
Media8b.ico
Debug
Bob.png
Crystal Italic1.ttf
Media Paused.ico
media7a.ico
media7b.ico
Media8a.ico
Media8b.ico
PVSLogoOutline.png
Subtitles Overlay
Various
PVS.AVPlayer Licenses
How To (C#)
PVS.AVPlayer Licenses
PVSAVPlayerHowTo
bin
Debug
PVS.AVPlayer.dll
PVS.AVPlayer.XML
Release
obj
Debug
Release
Properties
How To (VB.NET)
PVS.AVPlayer Licenses
PVSAVPlayerHowToVB
bin
Debug
PVS.AVPlayer.dll
PVS.AVPlayer.XML
Release
My Project
Application.myapp
obj
Debug
Release
PVSAVPlayerHowTo.vbproj.user
PVS.AVPlayer Licenses
PVS.AVPlayer Examples
AVPlayerExample.ex_
FolderView.ex_
AVPlayerExample.exe
FolderView.exe
PVS.AVPlayer Licenses
PVS.AVPlayer.dll
#region Usings

using PVS.AVPlayer;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Windows.Forms;

#endregion

namespace AVPlayerExample
{
    public partial class Mp3CoverOverlay : Form, IOverlay
    {
        /*
        
        PVS.AVPlayer Display Overlay - Example 'MP3 Cover'
        Displays mp3 artist, song and album information with albumart image (if available)
        when an mp3 mediafile is being played.

        The overlay reads the ID3 tag (if any) that has been added to many mp3 files.
        (It is not complete as it reads only ID3 version 1 and 2.3, but it seems to work for most files.
        The information about the ID3 tags comes from www.ID3.org.

        Some of the code for this overlay is a bit more complicated because the overlay has to be fully invisible
        when a movie (instead of an mp3) is being played and the background settings have to be remembered.
        
        */

        // ******************************** Fields

        #region Fields

        // The form and player
        private readonly Form1      _baseForm;
        private readonly Player     _basePlayer;

        // mp3 info
        private string              _artist;
        private string              _title;
        private string              _album;
        private Image               _cover;

        // drawing
        private bool                _activeState;

        // Used with method GetLocalAlbumArt()
        private DirectoryInfo       _directoryInfo;
        private string              _searchFileName;
        private string              _searchDirectoryName;
        private readonly string[]   _searchKeyWords = { "*front*", "*cover*"}; // , "*albumart*large*" };
        private readonly string[]   _searchExtensions = { ".jpg", ".jpeg", ".bmp", ".png", ".gif", ".tiff" };
        private bool                _skipEmbedPic; // if true skip mp3 file embedded album art (only show pic stored in folder)

        private bool                _menuOn;
        private bool                _reColor;
        private int                 _imageColor; // 0 = b/w, 1 = grayscale, 2 = sepia, 3 = inverse - (make enum)

        private bool                _hasBackImage;
        private bool                _hasHiddenBackImage;
        private string              _backImageName;
        private string              _backImagePath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);

        private Player              _backPlayer;
        private bool                _hasBackVideo;
        private TimeSpan            _backVideoPosition;
        private bool                _hasHiddenBackVideo;
        private string              _backVideoName;
        private string              _backVideoPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

        private bool                _disposed;

        #endregion

        // ******************************** Initialize and Form and Player event handling

        #region Initialize and Form and Player event handling

        public Mp3CoverOverlay(Form1 baseForm, Player basePlayer)
        {
            InitializeComponent();
            _baseForm = baseForm;
            _basePlayer = basePlayer;

            // Pass on drag and drop to main form (handled in source file: DragDrop.cs):
            AllowDrop = true;
            DragEnter += _baseForm.Form1_DragEnter;
            DragDrop += _baseForm.Form1_DragDrop;

            optionsPanel.Visible = false;

            ResizeRedraw = true;
        }

        private void MP3CoverOverlay_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible)
            {
                _basePlayer.MediaStarted += basePlayer_MediaStarted;
                // event media stopped is passed on by main app in IOverlay control
                if (_basePlayer.Playing && !_basePlayer.VideoPresent)
                {
                    SetActiveState(true);
                    Width--; Width++; // used because of repaint problems - see method SetActiveState
                }
                else SetActiveState(false);
            }
            else
            {
                _basePlayer.MediaStarted -= basePlayer_MediaStarted;
                SetActiveState(false);
            }
        }

        // see also: iOverlay Control: MediaStopped
        void basePlayer_MediaStarted(object sender, EventArgs e)
        {
            if (!_basePlayer.VideoPresent)
            {
                SetActiveState(true);
                Width--; Width++; // used because of repaint problems - see method SetActiveState
            }
            else SetActiveState(false);
        }

        protected override void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    if (_hasBackImage) RemoveBackImage(false);
                    if (_backPlayer != null) _backPlayer.Dispose();

                    if (components != null) components.Dispose();
                }
                base.Dispose(disposing);
                _disposed = true;
            }
        }

        #endregion

        // ******************************** iOverlay Control

        #region iOverlay Control

        // The visibility of the menu is controlled by the user from the main application (in this example application)
        public bool MenuEnabled
        {
            get { return _menuOn; }
            set
            {
                _menuOn = value;
                if (_activeState) optionsPanel.Visible = value;
            }
        }

        public bool HasMenu
        {
            get { return true; }
        }

        // The main application signals player media has ended (before playing new media)
        // This 'construction' is to prevent this overlay missing the player's MediaEnded event
        // before a (new) MediaStarted event is fired
        public void MediaStopped()
        {
            if (Visible) SetActiveState(false);
        }

        #endregion

        // ******************************** Options Menu

        #region Options Menu

        // text color
        private void textColorMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog {Color = topLabel.ForeColor};
            if (colorDialog.ShowDialog(this) == DialogResult.OK)
            {
                topLabel.ForeColor = bottomLabel.ForeColor = colorDialog.Color;
            }
            colorDialog.Dispose();
        }

        // background color
        private void backColorMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog {Color = BackColor};
            if (colorDialog.ShowDialog(this) == DialogResult.OK)
            {
                if (_hasBackImage) RemoveBackImage(false);
                else if (_hasBackVideo) RemoveBackVideo(false);

                BackColor = colorDialog.Color;
                if (!_activeState) TransparencyKey = BackColor;
            }
            colorDialog.Dispose();
        }

        // background image
        private void backgroundImageMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog
            {
                InitialDirectory = _backImagePath,
                Filter = "Images|*.bmp; *.jpg; *.jpeg; *.gif; *.tif; *.tiff; *.png|All Files|*.*"
            };
            if (dlg.ShowDialog(this) == DialogResult.OK)
            {
                _backImagePath = Path.GetDirectoryName(dlg.FileName);
                _hasHiddenBackImage = false;
                SetBackImage(dlg.FileName);
            }
            dlg.Dispose();
        }

        // background video
        private void backgroundVideoMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog
            {
                InitialDirectory = _backVideoPath,
                Filter = "Video|*.avi; *.divx; *.flv; *.h264; *.mp4; *.mpeg; *.mpeg4; *.mpg; *.wmv; *.xvid|All Files|*.*"
            };
            if (dlg.ShowDialog(this) == DialogResult.OK)
            {
                _backVideoPath = Path.GetDirectoryName(dlg.FileName);
                _hasHiddenBackVideo = false;
                SetBackVideo(dlg.FileName);
            }
            dlg.Dispose();
        }

        // cover (album art) source - enables to skip mp3 file embedded album art
        private void mP3AndFolderMenuItem_Click(object sender, EventArgs e)
        {
            _skipEmbedPic = false;
            mP3AndFolderMenuItem.Checked = true;
            folderOnlyMenuItem.Checked = false;
            if (_activeState)
            {
                if (_cover != null)
                {
                    _cover.Dispose();
                    _cover = null;
                }
                GetInfo();
            }
        }

        private void folderOnlyMenuItem_Click(object sender, EventArgs e)
        {
            _skipEmbedPic = true;
            mP3AndFolderMenuItem.Checked = false;
            folderOnlyMenuItem.Checked = true;
            if (_activeState)
            {
                if (_cover != null)
                {
                    _cover.Dispose();
                    _cover = null;
                }
                GetInfo();
            }
        }

        // cover (album art) color
        private void blackWhiteMenuItem_Click(object sender, EventArgs e)
        {
            _reColor = true;
            _imageColor = 0;
            SetcolorMenuCheckMarks((ToolStripMenuItem)sender);
        }

        private void imageGrayScaleMenuItem_Click(object sender, EventArgs e)
        {
            _reColor = true;
            _imageColor = 1;
            SetcolorMenuCheckMarks((ToolStripMenuItem)sender);
        }

        private void imageSepiaMenuItem_Click(object sender, EventArgs e)
        {
            _reColor = true;
            _imageColor = 2;
            SetcolorMenuCheckMarks((ToolStripMenuItem)sender);
        }

        private void imageInverseMenuItem_Click(object sender, EventArgs e)
        {
            _reColor = true;
            _imageColor = 3;
            SetcolorMenuCheckMarks((ToolStripMenuItem)sender);
        }

        private void imageNormalMenuItem_Click(object sender, EventArgs e)
        {
            _reColor = false;
            SetcolorMenuCheckMarks((ToolStripMenuItem)sender);
        }

        private void SetcolorMenuCheckMarks(ToolStripMenuItem checkItem)
        {
            blackWhiteMenuItem.Checked = false;
            imageGrayScaleMenuItem.Checked = false;
            imageSepiaMenuItem.Checked = false;
            imageInverseMenuItem.Checked = false;
            imageNormalMenuItem.Checked = false;

            checkItem.Checked = true;
            if (_activeState) GetInfo();
        }

        // restore default colors
        private void restoreDefaultsMenuItem_Click(object sender, EventArgs e)
        {
            SuspendLayout();

            if (_hasBackImage) RemoveBackImage(false);
            else if (_hasBackVideo) RemoveBackVideo(false);

            topLabel.ForeColor = bottomLabel.ForeColor = Color.FromArgb(189, 159, 87);
            topLabel.BackColor = bottomLabel.BackColor = imageBox1.BackColor = Color.Transparent;
            BackColor = Color.FromArgb(18, 18, 18);

            if (!_activeState) TransparencyKey = BackColor;

            _reColor = false;
            SetcolorMenuCheckMarks(imageNormalMenuItem);

            ResumeLayout();
        }

        #endregion

        // ******************************** SetActiveState / GetInfo

        #region SetActiveState / GetInfo

        // Don't show overlay contents when a movie (main player) is playing
        private void SetActiveState(bool state)
        {
            if (state)
            {
                if (_hasHiddenBackImage) SetBackImage(_backImageName);
                else if (_hasHiddenBackVideo) SetBackVideo(_backVideoName);

                GetInfo();
                if (_menuOn && !optionsPanel.Visible) optionsPanel.Visible = true;

                if (_hasBackVideo) TransparencyKey = BackColor;
                else TransparencyKey = Color.RosyBrown;

                _activeState = true;
                Invalidate(true);
            }
            else if (_activeState)
            {
                topLabel.Text = string.Empty;
                bottomLabel.Text = string.Empty;

                if (_hasBackImage) RemoveBackImage(true);
                else if (_hasBackVideo) RemoveBackVideo(true);

                imageBox1.Image = null;
                if (_cover != null)
                {
                    _cover.Dispose();
                    _cover = null;
                }

                optionsPanel.Visible = false;
                TransparencyKey = BackColor;

                _activeState = false;

                Invalidate(true);
            }
        }

        // Get mp3 info
        private void GetInfo()
        {
            GetMediaInfo(_basePlayer.GetMediaName(MediaName.FullPath));

            SuspendLayout();

            if (_album == string.Empty) topLabel.Text = _artist;
            else if (_artist == string.Empty) topLabel.Text = _album;
            else topLabel.Text = _artist + " - " + _album;
            bottomLabel.Text = _title;
            imageBox1.Image = _cover;

            ResumeLayout();
        }

        #endregion

        // ******************************** Get information from mp3 ID3 tags v1 or v2.3 (maybe other versions) / GetMediaInfo

        #region Get information from mp3 ID3 v1 or v2.3 (and maybe other versions) / GetMediaInfo

        private void GetMediaInfo(string mediaFile)
        {
            byte[] header = new byte[10];
            byte[] buffer = new byte[128];
            int found = 0;
            byte[] textEncoding = new byte[1];

            _artist = string.Empty;
            _title = string.Empty;
            _album = string.Empty;
            if (_cover != null)
            {
                _cover.Dispose();
                _cover = null;
            }

            if (!new Uri(mediaFile).IsFile)
            {
                _album = mediaFile;
                _title = Path.GetFileNameWithoutExtension(mediaFile);
                return;
            }

            FileStream fs = File.OpenRead(mediaFile);

            // read tag header ID3v2
            fs.Read(header, 0, 10);
            if (Encoding.Default.GetString(header, 0, 3) == "ID3")
            {
                int totalSize = header[9] + (0x80 * header[8]) + (0x8000 * header[7]) + (0x800000 * header[6]);
                // check for extended header
                int frameSize;
                if ((header[5] & 0x40) == 0x40)
                {
                    fs.Read(header, 0, 10);
                    frameSize = header[3] + (0x80 * header[2]) + (0x8000 * header[1]) + (0x800000 * header[0]);
                    fs.Position += frameSize;
                }

                while (fs.Position < totalSize)
                {
                    // read frameheader
                    fs.Read(header, 0, 10);
                    string frameId = Encoding.Default.GetString(header, 0, 4);
                    if (frameId == "APIC") frameSize = (0x1000000 * header[4]) + (0x10000 * header[5]) + (0x100 * header[6]) + header[7]; // differs from the description by ID3.org
                    else frameSize = (0x800000 * (header[4] & 0x7F)) + (0x8000 * (header[5] & 0x7F)) + (0x80 * (header[6] & 0x7F)) + (header[7] & 0x7F);
                    if ((header[9] & 0x60) != 0) fs.Read(textEncoding, 0, 1);
                    switch (frameId)
                    {
                        case "APIC": // picture
                            if (_cover != null || _skipEmbedPic)
                            {
                                fs.Position += frameSize;
                                break;
                            }

                            fs.Read(textEncoding, 0, 1);
                            int i = 0;
                            do
                            {
                                fs.Read(textEncoding, 0, 1);
                                buffer[i] = textEncoding[0];
                            }
                            while (buffer[i++] != 0);
                            int totalHeader = i;

                            fs.Read(textEncoding, 0, 1);
                            i = 0;
                            do
                            {
                                fs.Read(textEncoding, 0, 1);
                                buffer[i] = textEncoding[0];
                            }
                            while (buffer[i++] != 0);
                            totalHeader += i;

                            byte[] newBuffer = new byte[frameSize - totalHeader];
                            fs.Read(newBuffer, 0, frameSize - totalHeader);

                            MemoryStream ms = new MemoryStream(newBuffer);
                            try { _cover = Image.FromStream(ms); }
                            catch { }
                            ms.Close();
                            break;
                        case "TALB": // Album
                            if (frameSize > buffer.Length) buffer = new byte[frameSize];
                            fs.Read(buffer, 0, frameSize);
                            switch (buffer[1])
                            {
                                case 0xFF:
                                    _album = Encoding.Unicode.GetString(buffer, 1, frameSize - 1).TrimEnd('\0');
                                    break;
                                case 0xFE:
                                    _album = Encoding.BigEndianUnicode.GetString(buffer, 1, frameSize - 1).TrimEnd('\0');
                                    break;
                                default:
                                    _album = Encoding.Default.GetString(buffer, 1, frameSize - 1).TrimEnd('\0');
                                    break;
                            }
                            found++;
                            break;
                        case "TIT2": // Title
                            if (frameSize > buffer.Length) buffer = new byte[frameSize];
                            fs.Read(buffer, 0, frameSize);
                            switch (buffer[1])
                            {
                                case 0xFF:
                                    _title = Encoding.Unicode.GetString(buffer, 1, frameSize - 1).TrimEnd('\0');
                                    break;
                                case 0xFE:
                                    _title = Encoding.BigEndianUnicode.GetString(buffer, 1, frameSize - 1).TrimEnd('\0');
                                    break;
                                default:
                                    _title = Encoding.Default.GetString(buffer, 1, frameSize - 1).TrimEnd('\0');
                                    break;
                            }
                            found++;
                            break;
                        case "TPE1": // Lead Performers
                            if (frameSize > buffer.Length) buffer = new byte[frameSize];
                            fs.Read(buffer, 0, frameSize);
                            if (_artist == string.Empty)
                            {
                                switch (buffer[1])
                                {
                                    case 0xFF:
                                        _artist = Encoding.Unicode.GetString(buffer, 1, frameSize - 1).TrimEnd('\0');
                                        break;
                                    case 0xFE:
                                        _artist = Encoding.BigEndianUnicode.GetString(buffer, 1, frameSize - 1).TrimEnd('\0');
                                        break;
                                    default:
                                        _artist = Encoding.Default.GetString(buffer, 1, frameSize - 1).TrimEnd('\0');
                                        break;
                                }
                            }
                            found++;
                            break;
                        case "TPE2": // Band
                            if (frameSize > buffer.Length) buffer = new byte[frameSize];
                            fs.Read(buffer, 0, frameSize);
                            switch (buffer[1])
                            {
                                case 0xFF:
                                    _artist = Encoding.Unicode.GetString(buffer, 1, frameSize - 1).TrimEnd('\0');
                                    break;
                                case 0xFE:
                                    _artist = Encoding.BigEndianUnicode.GetString(buffer, 1, frameSize - 1).TrimEnd('\0');
                                    break;
                                default:
                                    _artist = Encoding.Default.GetString(buffer, 1, frameSize - 1).TrimEnd('\0');
                                    break;
                            }
                            found++;
                            break;
                        default:
                            fs.Position += frameSize;
                            break;
                    }
                }
            }

            // If nothing (or not all wanted info) found, try two other methods:
            if (found < 3)
            {
                // read tag ID3v1
                fs.Seek(-128, SeekOrigin.End);
                fs.Read(buffer, 0, 128);
                if (Encoding.Default.GetString(buffer, 0, 3) == "TAG")
                {
                    if (_title == string.Empty) _title = Encoding.Default.GetString(buffer, 3, 30).TrimEnd('\0');
                    if (_artist == string.Empty) _artist = Encoding.Default.GetString(buffer, 33, 30).TrimEnd('\0');
                }

                // Get info from file path
                if (_title == string.Empty) _title = Path.GetFileNameWithoutExtension(mediaFile);
            }
            fs.Close();
            //fs.Dispose();

            if (_cover == null) GetLocalAlbumArt(mediaFile);
            if (_cover != null && _reColor) _cover = ReColorImage((Bitmap)_cover);
        }

        #endregion

        // ******************************** Get information from mp3 directory files / GetLocalAlbumArt / SearchAlbumArt

        #region Get information from mp3 directory files / GetLocalAlbumArt / SearchAlbumArt

        private void GetLocalAlbumArt(string mediaFile)
        {
            _directoryInfo = new DirectoryInfo(Path.GetDirectoryName(mediaFile));
            _searchFileName = Path.GetFileNameWithoutExtension(mediaFile);
            _searchDirectoryName = _directoryInfo.Name; // how to check if this returns drive letter (root)?

            // 1. search using the file name
            if (!SearchAlbumArt(_searchFileName))
            {
                // 2. search using the directory name
                if (!SearchAlbumArt(_searchDirectoryName))
                {
                    // 3. search using keywords
                    int i = 0;
                    bool result;
                    do result = SearchAlbumArt(_searchKeyWords[i++]);
                    while (!result && i < _searchKeyWords.Length);

                    if (!result)
                    {
                        // 4. find largest file
                        SearchAlbumArt("*");
                    }
                }
            }
            _directoryInfo = null;
        }

        private bool SearchAlbumArt(string searchName)
        {
            if (searchName.EndsWith(@":\")) return false; // root directory - no folder name (_searchDirectoryName e.g. C:\)

            bool retVal = false;
            List<FileInfo> filesFound = new List<FileInfo>();

            // find all files
            for (int i = 0; i < _searchExtensions.Length; i++) filesFound.AddRange(_directoryInfo.GetFiles(searchName + _searchExtensions[i]));

            // if any, get the largest
            if (filesFound.Count > 0)
            {
                long length = 0;
                int index = 0;

                for (int j = 0; j < filesFound.Count; j++)
                {
                    if (filesFound[j].Length > length)
                    {
                        length = filesFound[j].Length;
                        index = j;
                    }
                }
                _cover = Image.FromFile(filesFound[index].FullName);
                filesFound.Clear();
                retVal = true;
            }
            return retVal;
        }

        #endregion

        // ******************************** Recolor cover image

        #region Recolor cover image

        // Method by and thanks to: Brandon Cannaday, Stephen Toub and others
        private Bitmap ReColorImage(Bitmap original)
        {
            ColorMatrix colorMatrix;

            // create a new bitmap the same size as original
            Bitmap newBitmap = new Bitmap(original.Width, original.Height);

            // get a graphics object from the new image
            Graphics g = Graphics.FromImage(newBitmap);

            switch (_imageColor)
            {
                case 1: // Grayscale - Matrix of type float
                    colorMatrix = new ColorMatrix(new[]
                    {
                        new[] {.3f, .3f, .3f, 0, 0},
                        new[] {.59f, .59f, .59f, 0, 0},
                        new[] {.11f, .11f, .11f, 0, 0},
                        new[] {0f, 0f, 0f, 1f, 0f},
                        new[] {0f, 0f, 0f, 0f, 1f}
                    });
                    break;

                case 2: // Sepia - Matrix of type float
                    colorMatrix = new ColorMatrix(new[]
                    {
                        new[] {.393f, .349f, .272f, 0, 0},
                        new[] {.769f, .686f, .534f, 0, 0},
                        new[] {.189f, .168f, .131f, 0, 0},
                        new[] {0f, 0f, 0f, 1f, 0f},
                        new[] {0f, 0f, 0f, 0f, 1f}
                    });
                    break;

                case 3: // Inverse - Matrix of type float
                    colorMatrix = new ColorMatrix(new[]
                    {
                        new[] {-1f, 0f, 0f, 0f, 0f},
                        new[] {0f, -1f, 0f, 0f, 0f},
                        new[] {0f, 0f, -1f, 0f, 0f},
                        new[] {0f, 0f, 0f, 1f, 0f},
                        new[] {1f, 1f, 1f, 0f, 1f}
                    });
                    break;

                default: // Black & White - Matrix of type float
                    colorMatrix = new ColorMatrix(new[]
                    {
                        new[] {1.5f, 1.5f, 1.5f, 0f, 0f},
                        new[] {1.5f, 1.5f, 1.5f, 0f, 0f},
                        new[] {1.5f, 1.5f, 1.5f, 0f, 0f},
                        new[] {0f, 0f, 0f, 1f, 0f},
                        new[] {-1f, -1f, -1f, 0f, 1f}
                    });
                    break;
            }

            // create some image attributes
            ImageAttributes attributes = new ImageAttributes();

            // set the color matrix attribute
            attributes.SetColorMatrix(colorMatrix);

            // draw the original image on the new image using the color matrix
            g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
               0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

            // dispose the Graphics object
            g.Dispose();

            // dispose the original bitmap
            original.Dispose();

            return newBitmap;
        }

        #endregion

        // ******************************** Set / Remove background image

        #region Set / Remove background image

        private void SetBackImage(string fileName)
        {
            if (_hasBackImage) RemoveBackImage(false);
            else if (_hasBackVideo) RemoveBackVideo(false);

            try
            {
                BackgroundImage = new Bitmap(fileName);
                _backImageName = fileName;
                _hasBackImage = true;
            }
            catch { }
        }

        private void RemoveBackImage(bool hideOnly)
        {
            if (_hasBackImage)
            {
                BackgroundImage.Dispose();
                BackgroundImage = null;
                _hasBackImage = false;
            }
            _hasHiddenBackImage = hideOnly;
        }

        #endregion

        // ******************************** Set / Remove background video / Create background player

        // As this is not really a 'true' overlay (there's no movie playing 'underneath')
        // we can use the main player's (unused) display to show a background movie

        #region Set / Remove background video / Create background player

        private void SetBackVideo(string fileName)
        {
            if (_backPlayer == null) CreateBackPlayer();

            if (_hasBackImage) RemoveBackImage(false);

            if (_hasHiddenBackVideo) _backPlayer.Play(fileName, _backVideoPosition, TimeSpan.Zero);
            else _backPlayer.Play(fileName);

            if (_backPlayer.LastError || !_backPlayer.VideoPresent)
            {
                _hasBackVideo = false;
            }
            else
            {
                BackColor = Color.RosyBrown;
                TransparencyKey = BackColor;
                _backVideoName = fileName;
                if (_hasHiddenBackVideo) _backPlayer.StartPositionMedia = TimeSpan.Zero; // reset startposition after continued from _backVideoPosition
                _hasBackVideo = true;
            }
            _hasHiddenBackVideo = false;
        }

        private void RemoveBackVideo(bool hideOnly)
        {
            if (_hasBackVideo)
            {
                _backVideoPosition = _backPlayer.Position;
                _backPlayer.Stop();
                _hasBackVideo = false;
            }
            _hasHiddenBackVideo = hideOnly;
        }

        private void CreateBackPlayer()
        {
            if (_backPlayer == null)
            {
                _backPlayer = new Player()
                {
                    Display = _basePlayer.Display,
                    DisplayMode = DisplayMode.Stretch,
                    AudioEnabled = false,
                    Repeat = true
                };
            }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Peter Vegter
United States United States
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190524.3 | Last Updated 21 May 2019
Article Copyright 2010 by Peter Vegter
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid