コード例 #1
0
        private static bool createImage(string srcName, string destName, int desiredWidth, int desiredHeight, Imgsize type)
        {
            var source      = GeneralConstants.APP_ROOT_DIR + srcName;
            var destination = GeneralConstants.APP_ROOT_DIR + destName;
            // Capture the original size of the uploaded image
            Image src = null;

            try
            {
#if AZURE
                src = Image.FromStream(photoContainer.GetBlobContentStream(srcName));
#else
                src = Image.FromFile(source);
#endif
            }
            catch (Exception ex)
            {
                if (src != null)
                {
                    src.Dispose();
                }
                Syslog.Write(ex);
                throw;
            }

            //Resize new image
            //Image tmp = src.GetThumbnailImage(desiredWidth, desiredHeight, null, IntPtr.Zero);
            Size  imgSize = new Size(desiredWidth, desiredHeight);
            Image tmp     = createResizedImage(src, imgSize, type);

            try
            {
                File.Delete(destination);
            }
            catch (Exception ex)
            {
                Syslog.Write(ex);
            }

            try
            {
#if AZURE
                var ms = new MemoryStream();
                tmp.Save(ms, destName.ToImageFormat());
                ms.Seek(0, SeekOrigin.Begin);
                photoContainer.AddBlobItem(destName, ms);
#else
                tmp.Save(destination, destination.ToImageFormat());
#endif
            }
            catch (Exception ex)
            {
                Syslog.Write(ex);
                Syslog.Write(destination);
                return(false);
            }
            finally
            {
                src.Dispose();
                tmp.Dispose();
            }

            return(true);
        }