Friday, July 8, 2016

Images


File Upload MVC   

  private JsonResult UploadFile(bool image = false)
        {

            var file = Request.Files[0];
            var path = image ? WebConfigurationManager.AppSettings["TempFacilityImagePath"] : WebConfigurationManager.AppSettings["TempFacilityDocPath"];
            if (file == null) return null;
            var name = Guid.NewGuid().ToString();
            string extension = Path.GetExtension(file.FileName);
            file.SaveAs($"{Server.MapPath(path)}{name}{extension}");
            return Json($"{name}{extension}", JsonRequestBehavior.AllowGet);
        }

function() {
            $(":file#physicalFiles").kendoUpload({
                async: {
                    saveUrl: "/admin/facility/uploaddoc",
                    autoUpload: true
                },
                multiple: false,
                success: function(e) {
                    var data = e.response;
                    $('#uploadedDocument').val(data);
                }
            });


-- convert byte array to image 

public static Image ToImage(this byte[] byteArrayIn)
        {
            using (var stream = new MemoryStream(byteArrayIn))
            {
                var image = Image.FromStream(stream);
                return image;
            }
        }

-- Resize Image

        public static Bitmap ResizeImage(this Image image, int width, int height)
        {
            var destRect = new Rectangle(0, 0, width, height);
            var destImage = new Bitmap(width, height);

            destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

            using (var graphics = Graphics.FromImage(destImage))
            {
                graphics.CompositingMode = CompositingMode.SourceCopy;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

                using (var wrapMode = new ImageAttributes())
                {
                    wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                    graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
                }
            }

            return destImage;
        }

-- convert image to byte array

        public static byte[] ToByteArray(this Image image)
        {
            using (var stream = new MemoryStream())
            {
                image.Save(stream, ImageFormat.Bmp);
                return stream.ToArray();
            }
         
        }



== create thub image

 Image image = Image.FromFile(fileName);
    Image thumb = image.GetThumbnailImage(120, 120, ()=>false, IntPtr.Zero);
    thumb.Save(Path.ChangeExtension(fileName, "thumb"));


Reduce image size without losing qality

Bitmap newImage = new Bitmap(newWidth, newHeight);
using (Graphics gr = Graphics.FromImage(newImage))
{
    gr.SmoothingMode = SmoothingMode.HighQuality;
    gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
    gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
    gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
}

No comments:

Post a Comment

Sql server row level policy