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 に答える