Click here to Skip to main content
15,881,173 members
Articles / Desktop Programming / Windows Forms

BSEtunes

Rate me:
Please Sign up or sign in to vote.
4.67/5 (11 votes)
24 Apr 2010CPOL4 min read 64.4K   4.3K   58  
BSEtunes is a MySQL based, full manageable, networkable single or multiuser jukebox application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Media;
using System.Windows.Threading;
using System.Windows.Media.Imaging;

namespace BSE.CoverFlow.WPFLib
{
    /// <summary>
    /// Thread class for getting an cover image  from the database and saving it into the
    /// cache directory below AppData.
    /// </summary>
    public class CreateImageSourceThread : BSE.Platten.Common.BaseThread
    {
        #region Events
        /// <summary>
        /// Occurs when the Imagesource is created.
        /// </summary>
        public event EventHandler<EventArgs> ImageSourceCreated;
        #endregion

        #region FieldsPrivate
        private readonly string m_strImageFileName;
        private readonly int m_iAlbumId;
        private readonly string m_strConnection;
        #endregion

        #region Properties
        #endregion

        #region MethodsPublic
        /// <summary>
        /// Default constructor of the BaseThread class.
        /// </summary>
        /// <param name="strImageFileName">The filename for the cover image.</param>
        /// <param name="iAlbumId">The AlbumId of the record in the database</param>
        /// <param name="strConnection">The connection string to the database</param>
        public CreateImageSourceThread(string strImageFileName, int iAlbumId, string strConnection)
            : base()
        {
            this.m_strImageFileName = strImageFileName;
            this.m_iAlbumId = iAlbumId;
            this.m_strConnection = strConnection;

            base.Thread = new System.Threading.Thread(
                new System.Threading.ThreadStart(this.CreateImageSource));
            base.Thread.Start();
        }
        #endregion

        #region MethodsProtected
        /// <summary>
        /// Raises the ThreadCompleted event.
        /// </summary>
        /// <param name="e">An EventArgs that contains the event data.</param>
        protected override void OnThreadCompleted(EventArgs e)
        {
            base.OnThreadCompleted(e);
            if (this.ImageSourceCreated != null)
            {
                this.ImageSourceCreated(this, EventArgs.Empty);
            }
        }
        #endregion

        #region MethodsPrivate
        private void CreateImageSource()
        {
            if (CoverItem.ExistsImageFile(this.m_strImageFileName) == false)
            {
                try
                {
                    BSE.Platten.BO.CCoversBusinessObject coversBusinessObject = new BSE.Platten.BO.CCoversBusinessObject(
                            m_strConnection);
                    if (coversBusinessObject != null)
                    {
                        using (System.Drawing.Image coverImage = coversBusinessObject.GetCoverImageByAlbumId(this.m_iAlbumId))
                        {
                            if (coverImage != null)
                            {
                                using (MemoryStream memoryStream = new MemoryStream())
                                {
                                    coverImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
                                    BitmapImage thumbNail = new BitmapImage();
                                    thumbNail.BeginInit();
                                    thumbNail.CacheOption = BitmapCacheOption.OnLoad;
                                    thumbNail.DecodePixelWidth = 300;
                                    thumbNail.StreamSource = memoryStream;
                                    thumbNail.EndInit();
                                    thumbNail.Freeze();

                                    using (FileStream fileStream = new FileStream(this.m_strImageFileName, FileMode.Create))
                                    {
                                        PngBitmapEncoder encoder = new PngBitmapEncoder();
                                        encoder.Interlace = PngInterlaceOption.On;
                                        encoder.Frames.Add(BitmapFrame.Create(thumbNail));
                                        encoder.Save(fileStream);
                                        encoder = null;
                                        thumbNail = null;
                                    }
                                }
                            }
                        }
                    }
                }
                catch
                { 
                }
            }
        }
        #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)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions