Пример #1
0
        /// <summary>
        /// The stream of the thumbnail file - computed once and cached in ThumbnailData
        /// </summary>
        public byte[] ThumbnailBytes()
        {
            if (m_ThumbBytes != null)
            {
                return(m_ThumbBytes);
            }

            using (Stream s = GetInputStream())
            {
                string szTempFile = null;
                try
                {
                    using (System.Drawing.Image image = MFBImageInfo.DrawingCompatibleImageFromStream(s, out szTempFile))
                    {
                        Info inf = MFBImageInfo.InfoFromImage(image);
                        using (Bitmap bmp = MFBImageInfo.BitmapFromImage(inf.Image, MFBImageInfo.ThumbnailHeight, MFBImageInfo.ThumbnailWidth))
                        {
                            using (MemoryStream sOut = new MemoryStream())
                            {
                                bmp.Save(sOut, ImageFormat.Jpeg);
                                m_ThumbBytes = sOut.ToArray();
                            }
                        }
                    }
                }
                finally
                {
                    if (!String.IsNullOrEmpty(szTempFile) && File.Exists(szTempFile))
                    {
                        File.Delete(szTempFile);
                    }
                }
            }
            return(m_ThumbBytes);
        }
Пример #2
0
        /// <summary>
        /// Creates the thumbnail from a completed video process.  Sets the width/height in the process.
        /// </summary>
        /// <param name="szBasePath">The base path for the video object</param>
        /// <param name="szPhysicalPath">The filename to use for the resulting thumbnail image</param>
        public void InitThumbnail(string szBasePath, string szPhysicalPath)
        {
            if (szBasePath == null)
            {
                throw new ArgumentNullException(nameof(szBasePath));
            }

            string szThumbFile = String.Format(CultureInfo.InvariantCulture, "{0}{1}00001{2}", MFBImageInfo.ThumbnailPrefixVideo, GUID, FileExtensions.JPG);

            if (szBasePath.StartsWith("/", StringComparison.Ordinal))
            {
                szBasePath = szBasePath.Substring(1);
            }
            string srcFile = szBasePath + szThumbFile;

            // Copy the thumbnail over
            using (IAmazonS3 s3 = AWSConfiguration.S3Client())
            {
                try
                {
                    using (GetObjectResponse gor = s3.GetObject(new GetObjectRequest()
                    {
                        BucketName = Bucket, Key = srcFile
                    }))
                    {
                        if (gor != null && gor.ResponseStream != null)
                        {
#pragma warning disable IDISP007 // Don't dispose injected
                            // Amazon documents EXPLICITLY say we should wrap in a using block.  See https://docs.aws.amazon.com/sdkfornet1/latest/apidocs/html/P_Amazon_S3_Model_S3Response_ResponseStream.htm
                            using (gor.ResponseStream)
#pragma warning restore IDISP007 // Don't dispose injected.
                            {
                                using (System.Drawing.Image image = MFBImageInfo.DrawingCompatibleImageFromStream(gor.ResponseStream))
                                {
                                    Info inf = MFBImageInfo.InfoFromImage(image);
                                    // save the thumbnail locally.
                                    inf.ImageDescription = Comment;

                                    Bitmap bmp = MFBImageInfo.BitmapFromImage(inf.Image, MFBImageInfo.ThumbnailHeight, MFBImageInfo.ThumbnailWidth);
                                    ThumbWidth  = bmp.Width;
                                    ThumbHeight = bmp.Height;

                                    using (bmp)
                                    {
                                        // get all properties of the original image and copy them to the new image.  This should include the annotation (above)
                                        foreach (PropertyItem pi in inf.Image.PropertyItems)
                                        {
                                            bmp.SetPropertyItem(pi);
                                        }

                                        bmp.Save(szPhysicalPath, ImageFormat.Jpeg);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (AmazonS3Exception)
                {
                    // Thumbnail was not found - audio file perhaps?  Use the generic audio file.
                    System.IO.File.Copy(HostingEnvironment.MapPath("~/images/audio.png"), szPhysicalPath);
                }


                // clean up the folder on S3 - anything that has the GUID but not .mp4 in it or the thumbnail in it.  (Save space!)  i.e., delete excess thumbnails and the source video file.
                List <S3Object>    lstS3Objects = new List <S3Object>();
                ListObjectsRequest loRequest    = new ListObjectsRequest()
                {
                    BucketName = Bucket, Prefix = szBasePath
                };
                // Get the list of S3 objects
                do
                {
                    ListObjectsResponse response = s3.ListObjects(loRequest);
                    foreach (S3Object o in response.S3Objects)
                    {
                        if (o.Key.Contains(GUID) && !o.Key.Contains(FileExtensions.MP4) && !o.Key.Contains(szThumbFile))
                        {
                            lstS3Objects.Add(o);
                        }
                    }

                    // If response is truncated, set the marker to get the next
                    // set of keys.
                    if (response.IsTruncated)
                    {
                        loRequest.Marker = response.NextMarker;
                    }
                    else
                    {
                        loRequest = null;
                    }
                } while (loRequest != null);

                lstS3Objects.ForEach((o) =>
                {
                    s3.DeleteObject(new DeleteObjectRequest()
                    {
                        BucketName = Bucket, Key = o.Key
                    });
                });
            }
        }
Пример #3
0
        /// <summary>
        /// Creates the thumbnail from a completed video process.  Sets the width/height in the process.
        /// </summary>
        /// <param name="szBasePath">The base path for the video object</param>
        /// <param name="szPhysicalPath">The filename to use for the resulting thumbnail image</param>
        public void InitThumbnail(string szBasePath, string szPhysicalPath)
        {
            if (szBasePath == null)
            {
                throw new ArgumentNullException(nameof(szBasePath));
            }

            string szThumbFile = String.Format(CultureInfo.InvariantCulture, "{0}{1}00001{2}", MFBImageInfo.ThumbnailPrefixVideo, GUID, FileExtensions.JPG);

            if (szBasePath.StartsWith("/", StringComparison.Ordinal))
            {
                szBasePath = szBasePath.Substring(1);
            }
            string srcFile = szBasePath + szThumbFile;

            // Copy the thumbnail over
            using (IAmazonS3 s3 = AWSConfiguration.S3Client())
            {
                GetObjectResponse gor = s3.GetObject(new GetObjectRequest()
                {
                    BucketName = Bucket, Key = srcFile
                });
                if (gor != null && gor.ResponseStream != null)
                {
                    using (gor.ResponseStream)
                    {
                        using (System.Drawing.Image image = MFBImageInfo.DrawingCompatibleImageFromStream(gor.ResponseStream))
                        {
                            Info inf = MFBImageInfo.InfoFromImage(image);

                            // save the thumbnail locally.
                            using (inf.Image)
                            {
                                inf.ImageDescription = Comment;

                                Bitmap bmp = MFBImageInfo.BitmapFromImage(inf.Image, MFBImageInfo.ThumbnailHeight, MFBImageInfo.ThumbnailWidth);
                                ThumbWidth  = bmp.Width;
                                ThumbHeight = bmp.Height;

                                using (bmp)
                                {
                                    // get all properties of the original image and copy them to the new image.  This should include the annotation (above)
                                    foreach (PropertyItem pi in inf.Image.PropertyItems)
                                    {
                                        bmp.SetPropertyItem(pi);
                                    }

                                    bmp.Save(szPhysicalPath, ImageFormat.Jpeg);
                                }
                            }
                        }
                    }
                }

                // clean up the folder on S3 - anything that has the GUID but not .mp4 in it or the thumbnail in it.  (Save space!)  i.e., delete excess thumbnails and the source video file.
                List <S3Object>    lstS3Objects = new List <S3Object>();
                ListObjectsRequest loRequest    = new ListObjectsRequest()
                {
                    BucketName = Bucket, Prefix = szBasePath
                };
                // Get the list of S3 objects
                do
                {
                    ListObjectsResponse response = s3.ListObjects(loRequest);
                    foreach (S3Object o in response.S3Objects)
                    {
                        if (o.Key.Contains(GUID) && !o.Key.Contains(FileExtensions.MP4) && !o.Key.Contains(szThumbFile))
                        {
                            lstS3Objects.Add(o);
                        }
                    }

                    // If response is truncated, set the marker to get the next
                    // set of keys.
                    if (response.IsTruncated)
                    {
                        loRequest.Marker = response.NextMarker;
                    }
                    else
                    {
                        loRequest = null;
                    }
                } while (loRequest != null);

                lstS3Objects.ForEach((o) =>
                {
                    s3.DeleteObject(new DeleteObjectRequest()
                    {
                        BucketName = Bucket, Key = o.Key
                    });
                });
            }
        }