C# 尝试调整大小和压缩高度,仅保持高宽比和高质量

C# 尝试调整大小和压缩高度,仅保持高宽比和高质量,c#,image,C#,Image,这里我正在处理从文件加载的图像 以及将小写名称转换为大写来覆盖目标图像 但在这里它的大小调整使用一个固定的大小,我需要调整高度260像素,并保持其纵横比不设置图像宽度 //--------------------------------------------------------------------------------// private void ProcessImage() { if (File.Exists(pictureBox1.ImageL

这里我正在处理从文件加载的图像 以及将小写名称转换为大写来覆盖目标图像 但在这里它的大小调整使用一个固定的大小,我需要调整高度260像素,并保持其纵横比不设置图像宽度

//--------------------------------------------------------------------------------//

    private void ProcessImage()
    {
        if (File.Exists(pictureBox1.ImageLocation))
        {
            string SourceImagePath = pictureBox1.ImageLocation;
            string ImageName = Path.GetFileName(SourceImagePath).ToUpper();
            string TargetImagePath = Properties.Settings.Default.ImageTargetDirectory + "\\" + ImageName;
           //Set the image to uppercase and save as uppercase
            if (SourceImagePath.ToUpper() != TargetImagePath.ToUpper())
            {

                using (Image Temp = Image.FromFile(SourceImagePath))
                {
                  // my problem is here, i need to resize only by height
                  // and maintain aspect ratio
                    Bitmap ResizedBitmap = resizeImage(Temp, new Size(175, 260));

                    //ResizedBitmap.Save(@TargetImagePath);
                    ResizedBitmap.Save(@TargetImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);

                }
                pictureBox1.ImageLocation = @TargetImagePath;
                File.Delete(SourceImagePath);
            }
        }
    }
    //--------------------------------------------------------------------------------//
    private static Bitmap resizeImage(Image imgToResize, Size size)
    {
        int sourceWidth = imgToResize.Width;
        int sourceHeight = imgToResize.Height;

        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;

        nPercentW = ((float)size.Width / (float)sourceWidth);
        nPercentH = ((float)size.Height / (float)sourceHeight);

        if (nPercentH < nPercentW)
            nPercent = nPercentH;
        else
            nPercent = nPercentW;

        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);

        Bitmap b = new Bitmap(destWidth, destHeight);
        Graphics g = Graphics.FromImage((Image)b);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;

        g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
        g.Dispose();

        return b;
    }
    //--------------------------------------------------------------------------------//
//--------------------------------------------------------------------------------//
私有void ProcessImage()
{
if(File.Exists(pictureBox1.ImageLocation))
{
字符串SourceImagePath=pictureBox1.ImageLocation;
字符串ImageName=Path.GetFileName(SourceImagePath.ToUpper();
字符串TargetImagePath=Properties.Settings.Default.ImageTargetDirectory+“\\”+ImageName;
//将图像设置为大写并另存为大写
if(SourceImagePath.ToUpper()!=TargetImagePath.ToUpper())
{
使用(Image Temp=Image.FromFile(SourceImagePath))
{
//我的问题是,我只需要按高度调整大小
//并保持纵横比
位图ResizedBitmap=resizeImage(临时,新大小(175260));
//ResizedBitmap.Save(@TargetImagePath);
ResizedBitmap.Save(@TargetImagePath,System.Drawing.Imaging.ImageFormat.Jpeg);
}
pictureBox1.ImageLocation=@TargetImagePath;
Delete(SourceImagePath);
}
}
}
//--------------------------------------------------------------------------------//
专用静态位图大小图像(图像imgToResize,大小)
{
int sourceWidth=imgToResize.Width;
int sourceHeight=imgToResize.Height;
浮动百分比=0;
浮动nPercentW=0;
浮点数nPercentH=0;
nPercentW=((float)size.Width/(float)sourceWidth);
nPercentH=((浮点)size.Height/(浮点)sourceHeight);
如果(nPercentH
您的意思是只想将图像调整到指定的高度,保持宽度成比例

    /// <summary>
    /// Resize the image to have the selected height, keeping the width proportionate.
    /// </summary>
    /// <param name="imgToResize"></param>
    /// <param name="newHeight"></param>
    /// <returns></returns>
    private static Bitmap resizeImage(Image imgToResize, int newHeight)
    {
        int sourceWidth = imgToResize.Width;
        int sourceHeight = imgToResize.Height; 

        float nPercentH = ((float)newHeight / (float)sourceHeight);

        int destWidth = Math.Max((int)Math.Round(sourceWidth * nPercentH), 1); // Just in case;
        int destHeight = newHeight;

        Bitmap b = new Bitmap(destWidth, destHeight);
        using (Graphics g = Graphics.FromImage((Image)b))
        {
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
        }

        return b;
    }

请注意,在保存时,调整大小算法的质量与文件压缩的质量不同。这两项行动都是成功的。我建议您使用最佳质量算法(更新后如上所示)调整大小,然后尝试保存质量,直到找到一个有效的算法。

本文有帮助吗?我发现这个库很容易使用:你的意思是你只想将图像调整到指定的高度,保持宽度成比例?是的,这是正确的大小图像代码工作得很好。但是上面的ProcessImage()代码我应该如何调整它的大小以保持70的质量和260的高度保持纵横比?@Garrick Oduber-“70的质量”-请定义这个术语。对默认c#库中可用的最高质量的大小调整算法提出了建议。例如,如果我在photoshop中调整大小,我会选择“质量高”,这将以良好的质量保存它,并且文件大小足够小。也许如果我可以在代码中选择尝试不同的质量,我可以进行测试,直到获得所需的质量,我正在尝试将电影封面调整为30/40kb大小。但是最大高度必须是260
    public static void SaveJpegWithSpecifiedQuality(this Image image, string filename, int quality)
    {
        // http://msdn.microsoft.com/en-us/library/ms533844%28v=vs.85%29.aspx
        // A quality level of 0 corresponds to the greatest compression, and a quality level of 100 corresponds to the least compression.
        if (quality < 0 || quality > 100)
        {
            throw new ArgumentOutOfRangeException("quality");
        }

        System.Drawing.Imaging.Encoder qualityEncoder = System.Drawing.Imaging.Encoder.Quality;
        EncoderParameters encoderParams = new EncoderParameters(1);
        EncoderParameter encoderParam = new EncoderParameter(qualityEncoder, (long)quality);
        encoderParams.Param[0] = encoderParam;

        image.Save(filename, GetEncoder(ImageFormat.Jpeg), encoderParams);
    }

    private static ImageCodecInfo GetEncoder(ImageFormat format)
    {
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
        foreach (ImageCodecInfo codec in codecs)
        {
            if (codec.FormatID == format.Guid)
            {
                return codec;
            }
        }
        return null;
    }