Click here to Skip to main content
15,892,674 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.7K   4.3K   58  
BSEtunes is a MySQL based, full manageable, networkable single or multiuser jukebox application
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data;
using System.Globalization;
using BSE.Platten.BO.Properties;

namespace BSE.Platten.BO
{
	/// <summary>
	/// Zusammendfassende Beschreibung f�r CCoverData.
	/// </summary>
	public class CCoverData
    {

        #region FieldsPrivate

        private string m_strInterpret;
		private string m_strTitel;
		private string m_strImageFileName;
		private Image m_imgCover;
        private Image m_imgThumbNail;
        private string m_strExtension;
		
		#endregion
		
		#region Properties

        public string Extension
		{
            get
            {
                if (this.m_strExtension.StartsWith(".", StringComparison.OrdinalIgnoreCase) == false)
                {
                    this.m_strExtension = "." + this.m_strExtension;
                }
                return this.m_strExtension;
            }
            set { this.m_strExtension = value; }
		}

		public string Interpret
		{
			get {return this.m_strInterpret;}
			set {this.m_strInterpret = value;}
		}

		public string Titel
		{
			get {return this.m_strTitel;}
			set {this.m_strTitel = value;}
		}

		public string ImageFileName
		{
			get {return this.m_strImageFileName;}
			set {this.m_strImageFileName = value;}
		}

		public Image Image
		{
			get {return this.m_imgCover;}
			set {this.m_imgCover = value;}
		}

        public Image ThumbNail
        {
            get { return this.m_imgThumbNail; }
            set { this.m_imgThumbNail = value; }
        }
        
        public static Size ThumbnailSize
        {
            get { return new Size(100, 100); }
        }
		#endregion

		#region MethodsPublic

		public CCoverData()
		{
		}

        public static bool IsAllowedCoverExtension(string strExtension)
        {
            System.Collections.ArrayList allowedCoverExtensions
                = new System.Collections.ArrayList(5);
            allowedCoverExtensions.Add(".bmp");
            allowedCoverExtensions.Add(".jpg");
            allowedCoverExtensions.Add(".jpeg");
            allowedCoverExtensions.Add(".gif");
            allowedCoverExtensions.Add(".png");

            bool bIsAllowedCoverExtension = false;
            if (String.IsNullOrEmpty(strExtension) == false)
            {
                if (allowedCoverExtensions.Contains(strExtension.ToLower(CultureInfo.InvariantCulture)) == true)
                {
                    bIsAllowedCoverExtension = true;
                }
            }
            return bIsAllowedCoverExtension;
        }

        public static Image GetImageFromFile(System.IO.FileSystemInfo fileInfo)
        {
            if (fileInfo == null)
            {
                throw new ArgumentNullException(
                    string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.IDS_ArgumentException,
                    "fileInfo"));
            }
            
            System.Drawing.Image image = null;
            try
            {
                using (System.IO.FileStream filestream = new System.IO.FileStream(
                    fileInfo.FullName,
                    System.IO.FileMode.Open,
                    System.IO.FileAccess.Read))
                {
                    Byte[] bytesPicture = new byte[filestream.Length];
                    filestream.Read(bytesPicture, 0, (int)filestream.Length);
                    image = GetImageFromBytes((byte[])bytesPicture);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return image;
        }

        public static Image GetThumbNailFromImage(Image image, Size imageSize)
        {
            if (image == null)
            {
                throw new ArgumentNullException(
                    string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.IDS_ArgumentException,
                    "image"));
            }
            
            System.Drawing.Image thumbImage = null;
            try
            {
                thumbImage = image.GetThumbnailImage(imageSize.Width, imageSize.Height, null, IntPtr.Zero);

            }
            catch (Exception)
            {
                throw;
            }
            return thumbImage;
        }

        public static Byte[] GetBytesFromImage(Image image, ImageFormat imageFormat)
        {
            if (image == null)
            {
                throw new ArgumentNullException(
                    string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.IDS_ArgumentException,
                    typeof(Image).Name));
            }
            
            Byte[] bytesFromImage = null;
            try
            {
                if (imageFormat == null)
                {
                    imageFormat = GetImageFormat(image);
                }
                using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
                {
                    image.Save(memoryStream, imageFormat);
                    bytesFromImage = new Byte[memoryStream.Length];
                    memoryStream.Position = 0;
                    memoryStream.Read(bytesFromImage, 0, (int)memoryStream.Length);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return bytesFromImage;
        }

        public static System.Drawing.Image GetImageFromBytes(Byte[] bytesToImage)
        {
            System.Drawing.Image image = null;
            try
            {
                image = Bitmap.FromStream(new System.IO.MemoryStream((byte[])bytesToImage));
            }
            catch (Exception)
            {
                throw;
            }
            return image;
        }

        public static System.Drawing.Image GetImageFromDbReader(
            IDataRecord dataReader,
            string strColumnName)
        {
            if (dataReader == null)
            {
                throw new ArgumentNullException(
                    string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.IDS_ArgumentException,
                    "dataReader"));
            }
            
            System.Drawing.Image image = null;
            try
            {
                object objectBlob = (object)dataReader[strColumnName];
                if (objectBlob != DBNull.Value)
                {
                    byte[] bytes = objectBlob as byte[];
                    if ((bytes != null) && (bytes.Length > 0))
                    {
                        image = GetImageFromBytes(bytes);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return image;
        }

        public static ImageFormat GetImageFormat(Image image)
        {
            if (image == null)
            {
                throw new ArgumentNullException(
                    string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.IDS_ArgumentException,
                    typeof(Image).Name));
            }
            
            ImageFormat imageFormat = image.RawFormat;
            if (imageFormat.Equals(ImageFormat.Bmp) == true)
            {
                imageFormat = ImageFormat.Bmp;
            }
            if (imageFormat.Equals(ImageFormat.Jpeg) == true)
            {
                imageFormat = ImageFormat.Jpeg;
            }
            if (imageFormat.Equals(ImageFormat.Gif) == true)
            {
                imageFormat = ImageFormat.Gif;
            }
            if (imageFormat.Equals(ImageFormat.Png) == true)
            {
                imageFormat = ImageFormat.Png;
            }
            return imageFormat;
        }

        public static Size GetCalculatedImageSize(Image image)
        {
            if (image == null)
            {
                throw new ArgumentNullException(
                    string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.IDS_ArgumentException,
                    typeof(Image).Name));
            }
            
            Size imageSize = ThumbnailSize;
            try
            {
                Single sThumbHeight = (Single)CCoverData.ThumbnailSize.Height;
                Single sThumbWidth = (Single)sThumbHeight / image.Height * image.Width;
                if (sThumbWidth > (Single)CCoverData.ThumbnailSize.Height)
                {
                    sThumbWidth = (Single)CCoverData.ThumbnailSize.Height;
                    sThumbHeight = (Single)sThumbWidth / image.Width * image.Height;
                }

                imageSize = new Size(Convert.ToInt32(sThumbWidth), Convert.ToInt32(sThumbHeight));
            }
            catch (Exception)
            {
                throw;
            }
            return imageSize;
        }

		#endregion

		#region MethodsPrivate

		#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