1

需要帮忙。我想调整某个图片的大小,但是当我运行此代码时,它给了我错误 {“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;
        }
4

2 回答 2

1

在您保存时,您的流仍然是打开的,open.FileName并且您无法保存具有相同名称的其他文件。您应该image.Dispose()释放句柄以便保存可以覆盖,或者您应该更改文件名。

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);
            } 
于 2012-11-22T09:21:29.077 回答
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);
}
于 2012-11-22T09:10:43.840 回答