How to compress/resize and display video in the view

时间:2019-01-18 18:47:06

标签: c# asp.net asp.net-mvc

I am displaying images and video as the thumbnail in the view page. Whenever a user uploads image or video I am saving URL into DB and files into the folder. While displaying I am resizing image size on the server side to reduce the page load time. I am able to compress the images and but not the videos. How to resize or compress the video same as the image. At present videos are not displaying because using the same logic to retrieve both image and videos.

View :

enter image description here

Database:

enter image description here

ImageHandler.ashx.cs :

namespace ResoucesProject
{
public class ImageHandler : IHttpHandler
{
    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        var fileName = ConfigurationManager.AppSettings["BasePath"] + context.Request.QueryString["file"];
        var filePath = context.Server.MapPath(fileName);
        var fileWidth = 300;
        if (!string.IsNullOrEmpty(context.Request.QueryString["width"]))
        {
            int tempWidth;
            if (int.TryParse(context.Request.QueryString["width"], out tempWidth) && tempWidth < 3000) // set a max limit so people don't request huge files
                fileWidth = tempWidth;
        }

        var fileHeight = 300;
        if (!string.IsNullOrEmpty(context.Request.QueryString["height"]))
        {
            int tempHeight;
            if (int.TryParse(context.Request.QueryString["height"], out tempHeight) && tempHeight < 3000) // set a max limit so people don't request huge files
                fileHeight = tempHeight;
        }

        context.Response.AddHeader("content-disposition",
             string.Format("attachment; filename={0}", fileName));

        if (File.Exists(filePath))
        {
            var buffer = GetResizedImage(filePath, fileWidth, fileHeight);
            if (buffer == null)
            {
                return;
            }

            context.Response.OutputStream.Write(buffer, 0, buffer.Length);
            context.Response.End();
            byte[] bytes = File.ReadAllBytes(filePath);
            context.Response.BinaryWrite(bytes);
        }
        else
        {
            throw new HttpException(404, "Invalid photo name.");
        }
    }

    private static byte[] GetResizedImage(string path, int width, int height)
    {
        try
        {
            var imgIn = new Bitmap(path);
            double y = imgIn.Height;
            double x = imgIn.Width;
            double factor = 1;
            if (width > 0)
            {
                factor = width / x;
            }
            else if (height > 0)
            {
                factor = height / y;
            }
            var outStream = new MemoryStream();
            var imgOut = new Bitmap((int)(x * factor), (int)(y * factor));

            // Set DPI of image (xDpi, yDpi)
            imgOut.SetResolution(72, 72);

            var g = Graphics.FromImage(imgOut);
            g.Clear(Color.White);
            g.DrawImage(imgIn, new Rectangle(0, 0, (int)(factor * x), (int)(factor * y)),
              new Rectangle(0, 0, (int)x, (int)y), GraphicsUnit.Pixel);

            imgOut.Save(outStream, GetImageFormat(path));
            return outStream.ToArray();
        }
        catch (ArgumentException e)
        {
            Console.WriteLine(e.Message);
            return null;
        }

    }

    private static ImageFormat GetImageFormat(string path)
    {
        switch (Path.GetExtension(path))
        {
            case ".bmp": return ImageFormat.Bmp;
            case ".gif": return ImageFormat.Gif;
            case ".jpg": return ImageFormat.Jpeg;
            case ".png": return ImageFormat.Png;
        }
        return ImageFormat.Jpeg;
    }
}

}

View :

@foreach (var item in Model)
                        {
                            <tr>
                                <td>
                                    <img src="~/ImageHandler.ashx?file=@Html.DisplayFor(modelItem =>item.image_url)&width=100&height=100" style="width:100px; height:100px;" />
                                </td>
                                                              
                            </tr>
                        }

0 个答案:

没有答案