示例#1
0
        /// <summary>
        /// Modifies the image.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        /// <param name="w">The w.</param>
        /// <param name="h">The h.</param>
        /// <param name="modType">Type of the mod.</param>
        /// <returns></returns>
        private string ModifyImage(int x, int y, int w, int h, ImageModificationType modType, int module)
        {
            //ResizeImage();
            ModifiedImageId = Guid.NewGuid();
            Image img = ImageHelper.ByteArrayToImage(ResizedImage);

            using (var bitmap = new Bitmap(w, h))
            {
                bitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);
                using (Graphics graphic = Graphics.FromImage(bitmap))
                {
                    graphic.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    graphic.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    graphic.PixelOffsetMode    = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                    graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                    if (modType == ImageModificationType.Crop)
                    {
                        graphic.DrawImage(img, 0, 0, w, h);
                        graphic.DrawImage(img, new Rectangle(0, 0, w, h), x, y, w, h, GraphicsUnit.Pixel);
                    }
                    else if (modType == ImageModificationType.Resize)
                    {
                        graphic.DrawImage(img, 0, 0, img.Width, img.Height);
                        graphic.DrawImage(img, new Rectangle(0, 0, W_FixedSize, H_FixedSize), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
                    }

                    string extension = WorkingImageExtension;

                    // If the image is a gif file, change it into png
                    if (extension.EndsWith("gif", StringComparison.OrdinalIgnoreCase))
                    {
                        extension = ".png";
                    }

                    using (var encoderParameters = new EncoderParameters(1))
                    {
                        encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
                        ModifiedImage = ImageHelper.ImageToByteArray(bitmap, extension, encoderParameters);
                        try
                        {
                            if (module == 2)
                            {
                                //  var returnValue = common.UpdateUserPhoto(int.Parse(Session["id"].ToString()), ModifiedImage);
                            }
                        }
                        catch (Exception)
                        {
                            throw new Exception("Image size is too large.");
                        }
                    }
                }
            }

            return(ModifiedImageId.ToString());
        }
示例#2
0
        /// <summary>
        /// Modifies an image image.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        /// <param name="w">The w.</param>
        /// <param name="h">The h.</param>
        /// <param name="modType">Type of the mod. Crop or Resize</param>
        /// <returns>New Image Id</returns>
        private string ModifyImage(int x, int y, int w, int h, ImageModificationType modType)
        {
            ModifiedImageId = Guid.NewGuid();
            Image img = ImageHelper.ByteArrayToImage(WorkingImage);

            using (System.Drawing.Bitmap _bitmap = new System.Drawing.Bitmap(w, h))
            {
                _bitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);
                using (Graphics _graphic = Graphics.FromImage(_bitmap))
                {
                    _graphic.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    _graphic.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    _graphic.PixelOffsetMode    = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                    _graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                    if (modType == ImageModificationType.Crop)
                    {
                        _graphic.DrawImage(img, 0, 0, w, h);
                        _graphic.DrawImage(img, new Rectangle(0, 0, w, h), x, y, w, h, GraphicsUnit.Pixel);
                    }
                    else if (modType == ImageModificationType.Resize)
                    {
                        _graphic.DrawImage(img, 0, 0, img.Width, img.Height);
                        _graphic.DrawImage(img, new Rectangle(0, 0, W_FixedSize, H_FixedSize), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
                    }

                    string extension = WorkingImageExtension;

                    // If the image is a gif file, change it into png
                    if (extension.EndsWith("gif", StringComparison.OrdinalIgnoreCase))
                    {
                        extension = ".png";
                    }

                    using (EncoderParameters encoderParameters = new EncoderParameters(1))
                    {
                        encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
                        ModifiedImage = ImageHelper.ImageToByteArray(_bitmap, extension, encoderParameters);
                    }
                }
            }

            return(ModifiedImageId.ToString());
        }
示例#3
0
        /// <summary>
        /// Modifies an image image.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        /// <param name="w">The w.</param>
        /// <param name="h">The h.</param>
        /// <param name="modType">Type of the mod. Crop or Resize</param>
        /// <returns>New Image Id</returns>
        private string ModifyImage(int x, int y, int w, int h, ImageModificationType modType, bool?withextension)
        {
            ModifiedImageId = Guid.NewGuid();
            Image img = ImageHelperNew.ByteArrayToImage(WorkingImage);
            int   genislik, yukseklik;

            if (Session["UploadType"] == null)
            {
                genislik  = Convert.ToInt32(Session["_minwidth2"].ToString());
                yukseklik = Convert.ToInt32(Session["_minheight2"].ToString());
            }
            else
            {
                genislik  = Convert.ToInt32(Session["_minwidth"].ToString());
                yukseklik = Convert.ToInt32(Session["_minheight"].ToString());
            }

            using (System.Drawing.Bitmap _bitmap = new System.Drawing.Bitmap(genislik, yukseklik))
            {
                _bitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);
                using (Graphics _graphic = Graphics.FromImage(_bitmap))
                {
                    _graphic.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    _graphic.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    _graphic.PixelOffsetMode    = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                    _graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                    if (modType == ImageModificationType.Crop)
                    {
                        //_graphic.DrawImage(img, 0, 0, w, h);
                        //_graphic.DrawImage(img, new Rectangle(0, 0, w, h), x, y, w, h, GraphicsUnit.Pixel);

                        // Aşağıdaki kodu ben ekliyorum. Crop işleminden sonra resize etmek için..


                        _graphic.DrawImage(img, new Rectangle(0, 0, genislik, yukseklik), x, y, w, h, GraphicsUnit.Pixel);
                    }
                    else if (modType == ImageModificationType.Resize)
                    {
                        _graphic.DrawImage(img, 0, 0, img.Width, img.Height);
                        _graphic.DrawImage(img, new Rectangle(0, 0, W_FixedSize, H_FixedSize), 0, 0, img.Width, 0, GraphicsUnit.Pixel);
                    }

                    string extension = WorkingImageExtension;

                    // If the image is a gif file, change it into png
                    if (extension.EndsWith("gif", StringComparison.OrdinalIgnoreCase))
                    {
                        extension = ".png";
                    }

                    using (EncoderParameters encoderParameters = new EncoderParameters(1))
                    {
                        encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
                        ModifiedImage = ImageHelperNew.ImageToByteArray(_bitmap, extension, encoderParameters);
                    }
                }
            }

            Image CoreppedImg = ImageHelperNew.ByteArrayToImage(ModifiedImage);

            //CoreppedImg.Save(Server.MapPath("/Content/images/userfiles/newbig/" + ModifiedImageId + WorkingImageExtension));
            //CoreppedImg.Save(Server.MapPath("/Content/images/userfiles/news/" + ModifiedImageId));

            if (withextension == false)
            {
                CoreppedImg.Save(Server.MapPath("/Content/images/userfiles/news/" + ModifiedImageId));
            }
            else
            {
                CoreppedImg.Save(Server.MapPath("/Content/images/userfiles/news/" + ModifiedImageId + WorkingImageExtension));
            }
            return(ModifiedImageId.ToString());
        }