Ejemplo n.º 1
0
        ///<summary>
        /// Image Resize
        ///</summary>
        ///<param name="image">The image you want to resize</param>
        ///<param name="path">Path to store the image</param>
        ///<param name="fileName">Filename for the image</param>
        ///<param name="cell">WebGrid RowCell</param>
        ///<returns></returns>
        ///<exception cref="GridException"></exception>
        public string Resize(System.Drawing.Image image, string path, string fileName, RowCell cell)
        {
            if (!string.IsNullOrEmpty(Directory))
            {
                path = Directory;
            }
            if (!string.IsNullOrEmpty(FileName))
            {
                fileName = FileName;
            }

            if (Width < 1)
            {
                throw new ArgumentException("Width is 0 or less");
            }
            if (Height < 1)
            {
                throw new ArgumentException("Height is 0 or less");
            }
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentException("FileName is empty");
            }
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException("Directory is empty");
            }
            if (image == null)
            {
                throw new ArgumentException("Image is null");
            }
            if (cell != null && cell.Row != null && fileName.Contains("["))
            {
                string tmp_file = fileName;
                cell.Row.Columns.ForEach(delegate(Column column)
                {
                    if (
                        tmp_file.IndexOf(string.Format("[{0}]", column.ColumnId),
                                         StringComparison.OrdinalIgnoreCase) == -1)
                    {
                        return;
                    }
                    if (cell.Row[column.ColumnId].Value != null)
                    {
                        tmp_file =
                            tmp_file.Replace(string.Format("[{0}]", column.ColumnId),
                                             cell.Row[column.ColumnId].Value.ToString());
                    }
                    else
                    {
                        tmp_file =
                            tmp_file.Replace(string.Format("[{0}]", column.ColumnId),
                                             "");
                    }
                });
                fileName = tmp_file;
            }
            if (!string.IsNullOrEmpty(Path.GetExtension(fileName)))
            {
                fileName = fileName.Replace(Path.GetExtension(fileName), "");
            }


            string ret;

            System.Drawing.Image bitMapImage = null;



            try
            {
                if (CustomGraphics != null)
                {
                    bitMapImage = new Bitmap(image.Width, image.Height, CustomGraphics);
                    CustomGraphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height));
                }
                else
                {
                    bitMapImage = ResizeImage(image, Width, Height, KeepAspectRatio, EnlargeSmallerImages);
                }

                string file = UseFileName
                                      ? string.Format("{0}{1}{2}{3}.{4}", path, Prefix, fileName, Suffix, ImageFormat)
                                      :
                              string.Format("{0}{1}{2}.{3}", path, Prefix, Suffix, ImageFormat);
                if (System.IO.File.Exists(file))
                {
                    System.IO.File.Delete(file);
                }
                bitMapImage.Save(file, ImageFormat);
                ret = path + Prefix + fileName + Suffix;
            }
            catch (Exception ee)
            {
                throw new GridException(
                          string.Format("Error resizing image file: {0}",
                                        string.Format("{0}{1}{2}{3}.{4}", path, Prefix, fileName, Suffix, ImageFormat)),
                          ee);
            }
            finally
            {
                if (bitMapImage != null)
                {
                    bitMapImage.Dispose();
                }
            }
            return(ret);
        }