/// <summary> /// Gets the byte array from stream. /// </summary> /// <param name="media">The media.</param> /// <param name="rpu">The rpu.</param> /// <param name="caller">The caller.</param> /// <returns></returns> /// <remarks>Documented by Dev05, 2009-01-16</remarks> private static byte[] GetByteArrayFromStream(System.IO.Stream media, MLifter.DAL.Tools.StatusMessageReportProgress rpu, object caller) { int buffer_length = 10240; byte[] data = new byte[media.Length]; StatusMessageEventArgs args = new StatusMessageEventArgs(StatusMessageType.CreateMediaProgress, (int)media.Length); media.Seek(0, SeekOrigin.Begin); int read = 0; int pos = 0; do { read = media.Read(data, pos, Math.Min(buffer_length, data.Length - pos)); pos += read; args.Progress = pos; if (rpu != null) { rpu.Invoke(args, caller); } }while (read == buffer_length); return(data); }
/// <summary> /// Creates a new media object. /// </summary> /// <param name="media">The memory stream containing the media.</param> /// <param name="type">The media type.</param> /// <param name="rpu">A delegate of type <see cref="StatusMessageReportProgress"/> used to send messages back to the calling object.</param> /// <param name="caller">The calling object.</param> /// <returns>The id for the new media object.</returns> /// <remarks>Documented by Dev03, 2008-08-05</remarks> /// <remarks>Documented by Dev03, 2009-01-13</remarks> public int CreateMedia(System.IO.Stream media, MLifter.DAL.Interfaces.EMedia type, MLifter.DAL.Tools.StatusMessageReportProgress rpu, object caller) { int newId; SqlCeCommand cmd = MSSQLCEConn.CreateCommand(Parent.CurrentUser); cmd.CommandText = "INSERT INTO MediaContent (data, media_type) VALUES (@data, @type); SELECT @@IDENTITY;"; cmd.Parameters.Add("@data", SqlDbType.Image); cmd.Parameters.Add("@type", SqlDbType.NVarChar, 100); cmd.Parameters["@data"].Value = GetByteArrayFromStream(media, rpu, caller); cmd.Parameters["@type"].Value = type.ToString(); newId = Convert.ToInt32(MSSQLCEConn.ExecuteScalar(cmd)); return(newId); }