Пример #1
0
        public static void SendImageThumbnailAsStream(Stream stream, string filename, ImageFileKind imageFileKind)
        {
            string origFullFileName = Path.Combine(Files.UploadDirectoryPath, listImageFileDimensions.Where(ifd => ifd.ImageKind == ImageFileKind.Original).SingleOrDefault().DirectoryToSave, filename);

            ImageFileDimension imageFileDimension = listImageFileDimensions.Where(ifd => ifd.ImageKind == imageFileKind).FirstOrDefault();

            using (Image bitmap = Bitmap.FromFile(origFullFileName))
            {
                int origHeight = bitmap.Height;
                int origWidth  = bitmap.Width;

                int newHeight = imageFileDimension.Height.Value;
                int newWidth  = imageFileDimension.Width.Value;

                float widthRatio  = ((float)newWidth) / origWidth;
                float heightRatio = ((float)newHeight) / origHeight;

                float ratio = (widthRatio < heightRatio ? widthRatio : heightRatio);// (widthRatio)MIN(maxWidth / Width, maxHeight / Height);

                if (ratio >= 1.0f)
                {
                    bitmap.Save(stream, allowedImageFiles[Path.GetExtension(origFullFileName)]);
                }
                else
                {
                    using (Image resultBitmap = bitmap.GetThumbnailImage((int)(origWidth * ratio), (int)(origHeight * ratio), null, IntPtr.Zero))
                    {
                        resultBitmap.Save(stream, allowedImageFiles[Path.GetExtension(origFullFileName)]);
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Get directory path by image kind.
        /// </summary>
        /// <param name="imageFileKind"></param>
        /// <param name="includeFullPath"></param>
        /// <returns></returns>
        public static string GetDirectoryToSaveWebPath(string directory)
        {
            ImageFileDimension dimensions = listImageFileDimensions.Where(ifd => ifd.DirectoryToSave == directory).FirstOrDefault();

            if (dimensions != null)
            {
                return(Path.Combine(Files.uploadFileDirectory, dimensions.DirectoryToSave));
            }
            else
            {
                return(String.Empty);
            }
        }
Пример #3
0
        /// <summary>
        /// Resize image file and save it with new unique file name.
        /// </summary>
        /// <param name="bitmap">Bitmap instance.</param>
        /// <param name="newFileName">Unique file name.</param>
        /// <param name="imageFileDimension">ImageFileDimension instance.</param>
        public static void ResizeImageFile(Bitmap bitmap, string newFileName, ImageFileDimension imageFileDimension)
        {
            int origHeight = bitmap.Height;
            int origWidth  = bitmap.Width;

            int newWidth  = origWidth;
            int newHeight = origHeight;

            int?targetHeight = imageFileDimension.Height;
            int?targetWidth  = imageFileDimension.Width;

            string      fullFileName = Path.Combine(Files.UploadDirectoryPath, imageFileDimension.DirectoryToSave, newFileName);
            ImageFormat imageFormat  = allowedImageFiles[Path.GetExtension(newFileName)];

            if (imageFileDimension.Width == null && imageFileDimension.Height == null)
            {
                bitmap.Save(fullFileName, imageFormat);
            }
            else
            {
                if (imageFileDimension.Width == null)
                {
                    newHeight = imageFileDimension.Height.Value;

                    if (origHeight > newHeight)
                    {
                        float heightRatio = (float)newHeight / origHeight;
                        newWidth = (int)(origWidth * heightRatio);
                    }
                    else
                    {
                        newHeight = origHeight;
                    }

                    targetWidth  = newWidth;
                    targetHeight = newHeight;
                }
                else if (imageFileDimension.Height == null)
                {
                    newWidth = imageFileDimension.Width.Value;

                    if (origWidth > newWidth)
                    {
                        float widthRatio = (float)newWidth / origWidth;
                        newHeight = (int)(origHeight * widthRatio);
                    }
                    else
                    {
                        newWidth = origWidth;
                    }

                    targetWidth  = newWidth;
                    targetHeight = newHeight;
                }
                else
                {
                    newHeight = imageFileDimension.Height.Value;
                    newWidth  = imageFileDimension.Width.Value;

                    float widthRatio  = ((float)newWidth) / origWidth;
                    float heightRatio = ((float)newHeight) / origHeight;

                    float ratio = (widthRatio > heightRatio ? widthRatio : heightRatio);// (widthRatio)MIN(maxWidth / Width, maxHeight / Height);

                    if (!imageFileDimension.IsCropped)
                    {
                        if (ratio < 1.0f)
                        {
                            newWidth  = (int)(origWidth * ratio);
                            newHeight = (int)(origHeight * ratio);
                        }
                        else
                        {
                            targetWidth  = newWidth = origWidth;
                            targetHeight = newHeight = origHeight;
                        }
                    }
                    else
                    {
                        newWidth  = (int)(origWidth * ratio);
                        newHeight = (int)(origHeight * ratio);
                    }
                }

                Rectangle?rectF = null;
                if (imageFileDimension.IsCropped)
                {
                    rectF = new Rectangle(0, 0, targetWidth.Value, targetHeight.Value);
                }

                SaveResizedImageFile(bitmap, fullFileName, newWidth, newHeight, imageFormat, rectF);
            }
        }