Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在上传过程中,调整和优化网页中图像的最佳类别是什么?_C#_Asp.net_Image_Visual Studio 2010_Resize - Fatal编程技术网

C# 在上传过程中,调整和优化网页中图像的最佳类别是什么?

C# 在上传过程中,调整和优化网页中图像的最佳类别是什么?,c#,asp.net,image,visual-studio-2010,resize,C#,Asp.net,Image,Visual Studio 2010,Resize,我正在寻找一个类(使用c#),它可以检查我的图像大小(宽度和高度)(或以kb为单位的图像大小),如果它们与我最喜欢的大小不匹配,请调整它们的大小(使用约束和相对宽度和高度表示新大小,如photoshop image size=中所示,这样我们就不会丢失图像外观) 这项工作有很多课程,但哪一个更好 感谢未来的发展似乎在这些地区受到高度重视 该问题的其他答案包括指向其他几个选项的链接,包括。这是我在指定所需宽度时用于调整大小的方法: private Image ResizeImage(Image o

我正在寻找一个类(使用c#),它可以检查我的图像大小(宽度和高度)(或以kb为单位的图像大小),如果它们与我最喜欢的大小不匹配,请调整它们的大小(使用约束和相对宽度和高度表示新大小,如photoshop image size=中所示,这样我们就不会丢失图像外观)

这项工作有很多课程,但哪一个更好

感谢未来的发展

似乎在这些地区受到高度重视


该问题的其他答案包括指向其他几个选项的链接,包括。

这是我在指定所需宽度时用于调整大小的方法:

private Image ResizeImage(Image original, int targetWidth)
{
    double percent = (double)original.Width / targetWidth;
    int destWidth = (int)(original.Width / percent);
    int destHeight = (int)(original.Height / percent);

    Bitmap b = new Bitmap(destWidth, destHeight);
    Graphics g = Graphics.FromImage((Image)b);
    try
    {
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        g.CompositingQuality = CompositingQuality.HighQuality;

        g.DrawImage(original, 0, 0, destWidth, destHeight);
    }
    finally
    {
        g.Dispose();
    }

    return (Image)b;
}
每个人,至少根据他们的电子邮件:)它是免费的,并且它包含了你想要做的事情的示例代码。clsImageResize.Resize(Server.MapPath(imgPath),50,50);
public static class clsImageResize
{
    public static int TnewWidth { get; set; }
    public static int TnewHeight { get; set; }
    public static string TfilePath { get; set; }

    public static void Resize(string filePath, int newWidth, int newHeight)
    {
        TfilePath = filePath;
        TnewWidth = newWidth;
        TnewHeight = newHeight;
        Bitmap image = new Bitmap(TfilePath);
        //Image m = Image.FromFile(TfilePath);
        Save(image, TnewWidth, TnewHeight, 8, TfilePath);
    }

    private static void Save(Bitmap image, int maxWidth, int maxHeight, int quality, string filePath)
    {
        // Get the image's original width and height
        int originalWidth = image.Width;
        int originalHeight = image.Height;

        // To preserve the aspect ratio
        float ratioX = (float)maxWidth / (float)originalWidth;
        float ratioY = (float)maxHeight / (float)originalHeight;
        float ratio = Math.Min(ratioX, ratioY);

        // New width and height based on aspect ratio
        int newWidth = (int)(originalWidth * ratio);
        int newHeight = (int)(originalHeight * ratio);

        // Convert other formats (including CMYK) to RGB.
        Bitmap newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);

        // Draws the image in the specified size with quality mode set to HighQuality
        using (Graphics graphics = Graphics.FromImage(newImage))
        {
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.DrawImage(image, 0, 0, newWidth, newHeight);
        }

        // Get an ImageCodecInfo object that represents the JPEG codec.
        ImageCodecInfo imageCodecInfo = GetEncoderInfo(ImageFormat.Jpeg);

        // Create an Encoder object for the Quality parameter.
        Encoder encoder = Encoder.Quality;

        // Create an EncoderParameters object. 
        EncoderParameters encoderParameters = new EncoderParameters(1);

        image.Dispose();
        // Save the image as a JPEG file with quality level.
        EncoderParameter encoderParameter = new EncoderParameter(encoder, quality);
        encoderParameters.Param[0] = encoderParameter;
        newImage.Save(filePath, imageCodecInfo, encoderParameters);
    }

    private static ImageCodecInfo GetEncoderInfo(ImageFormat format)
    {
        return ImageCodecInfo.GetImageDecoders().SingleOrDefault(c => c.FormatID == format.Guid);
    }
}