图像大小调整至少是其原始大小的4倍 [英] Image resizing is taking at least 4 times its original size

查看:66
本文介绍了图像大小调整至少是其原始大小的4倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在使用的代码:

This is the code which I am using :

//on button click
private void button1_Click(object sender, EventArgs e)
{

    string rootFolderPath = @"E:\\sujesh\\ULLAS_WorkFolder\\HOTEL IMAGES\\HotelImages_HPO\\HPO\\";
    string destinationPath = @"C:\\resized_image\\";
   
    ResizeImagesInFolder(rootFolderPath, destinationPath, 260);

}

//this the function
 public void ResizeImagesInFolder(string SourceFolder, string DestinationFolder, int NewImageSize)
{
    try
    {
        // Check if source folder exists and throw exception if not
        if (!Directory.Exists(SourceFolder))
            throw new Exception("SourceFolder does not exist");

        // Check if destination folder exists, but create it if not
        if (!Directory.Exists(DestinationFolder))
        {
            Directory.CreateDirectory(DestinationFolder);
        }

        // List all images from source directory
        DirectoryInfo diImages = new DirectoryInfo(SourceFolder);
        ArrayList alImages = new ArrayList();
      
        alImages.AddRange(diImages.GetFiles("*.gif"));
        alImages.AddRange(diImages.GetFiles("*.jpg"));
        alImages.AddRange(diImages.GetFiles("*.bmp"));
        alImages.AddRange(diImages.GetFiles("*.png"));

        Image imgOriginal;
        float OriginalHeight;
        float OriginalWidth;
        int NewWidth;
        int NewHeight;
        Bitmap ResizedBitmap;
        Graphics ResizedImage;
        MessageBox.Show("start");
        // Resize every image
        foreach (FileInfo fiImage in alImages)
        {
            // Loads original image from source folder
            imgOriginal = Image.FromFile(fiImage.FullName);
            // Finds height and width of original image
            OriginalHeight = imgOriginal.Height;
            OriginalWidth = imgOriginal.Width; 
            NewHeight = 160;
            NewWidth = 270;                  
            ResizedBitmap = new Bitmap(NewWidth, NewHeight);
            ResizedImage = Graphics.FromImage(ResizedBitmap);
            ResizedImage.DrawImage(imgOriginal, 0, 0, 260, 170);
            ResizedBitmap.Save(DestinationFolder + fiImage.Name);
            imgOriginal.Dispose();
            ResizedBitmap.Dispose();
            ResizedImage.Dispose();
        }
        MessageBox.Show("Successful");

    }
    catch (Exception ex)
    {
        MessageBox.Show("Unsuccessful because of " + ex.ToString());

    }
}



如何调整图像大小但是尺寸不应该增加太多


What is to be done to for resizing the image but the size should not increase that much

推荐答案

新高度和新宽度是硬编码的。

The New Height and New Width are hardcoded.
NewHeight = 160;
NewWidth = 270;



再次, NewImageSize 参数永远不会在方法中使用。


Again, NewImageSize parameter is never used inside the method.

public void ResizeImagesInFolder(string SourceFolder, string DestinationFolder, int NewImageSize)


这篇关于图像大小调整至少是其原始大小的4倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆