调整图片大小C#

时间:2012-11-22 08:59:29

标签: c#

需要帮助。我想调整某个图片的大小,但是当我运行此代码时,它会给我错误{“GDI +中发生一般错误。”}

 private void btnPic_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Images|*.bmp;*.jpg;*.gif|All files|*.*";

            if (open.ShowDialog(this) == DialogResult.OK)
            {

                 var image = Image.FromFile(open.FileName);
                 var newImage = ScaleImage(image, 300, 400);
                 newImage.Save(@open.FileName, ImageFormat.Png);

            }
}


 public static Image ScaleImage(Image image, int maxWidth, int maxHeight)
        {
            var ratioX = (double)maxWidth / image.Width;
            var ratioY = (double)maxHeight / image.Height;
            var ratio = Math.Min(ratioX, ratioY);

            var newWidth = (int)(image.Width * ratio);
            var newHeight = (int)(image.Height * ratio);

            var newImage = new Bitmap(newWidth, newHeight);
            Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
            return newImage;
        }

2 个答案:

答案 0 :(得分:1)

您可以尝试使用此版本。

private void btnPic_Click(object sender, EventArgs e)
{
    OpenFileDialog open = new OpenFileDialog();
    open.Filter = "Images|*.bmp;*.jpg;*.gif|All files|*.*";

    if (open.ShowDialog(this) == DialogResult.OK)
    {

         var image = Image.FromFile(open.FileName);
         var newImage = ResizeCenterCropped(image, 300, 400);
         newImage.Save(@open.FileName, ImageFormat.Png);

    }
}   

public static Image ResizeCenterCropped(Image image, int width, int height)
{
    var rect = CreateCroppedRectangle(image, width, height);
    rect.X = (image.Width / 2) - (rect.Width / 2);
    rect.Y = (image.Height / 2) - (rect.Height / 2);
    return Resize(image, new Rectangle(0, 0, width, height), rect);
}

public static Image Resize(Image image, Rectangle destRectange, Rectangle sourceRectangle)
{
    var rezisedImage = new Bitmap(destRectange.Width, destRectange.Height)
    using (var g = Graphics.FromImage(rezisedImage))
    {
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        g.DrawImage(image, destRectange, sourceRectangle, GraphicsUnit.Pixel);
        return rezisedImage;
    }
}

public static Rectangle CreateCroppedRectangle(Image image, int width, int height)
{
    var size = new Size(width, height);
    var size2 = new Size(image.Width, image.Height);

    //The maximum scale width we could use
    float maxWidthScale = (float)size2.Width / (float)size.Width;

    //The maximum scale height we could use
    float maxHeightScale = (float)size2.Height / (float)size.Height;

    //Use the smaller of the 2 scales for the scaling
    float scale = Math.Min(maxHeightScale, maxWidthScale);


    size.Width = (int)(size.Width * scale);
    size.Height = (int)(size.Height * scale);

    return new Rectangle(new Point(), size);
}

答案 1 :(得分:1)

当您保存时,您的信息流仍然为open.FileName打开,您无法使用相同的名称保存其他文件。您应该image.Dispose()释放句柄,以便Save可以覆盖,或者您应该更改文件名。

private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Images|*.bmp;*.jpg;*.gif|All files|*.*";

            if (open.ShowDialog(this) == DialogResult.OK)
            {

                var image = Image.FromFile(open.FileName);
                var newImage = ScaleImage(image, 300, 400);
                image.Dispose();//Add this to your code
                newImage.Save(open.FileName, ImageFormat.Png);
            }

或更改文件名

private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Images|*.bmp;*.jpg;*.gif|All files|*.*";

            if (open.ShowDialog(this) == DialogResult.OK)
            {

                var image = Image.FromFile(open.FileName);
                var newImage = ScaleImage(image, 300, 400);
                string path = "1.png";
                newImage.Save(open.FileName, ImageFormat.Png);
            }