Saving An image,A generic error occurred in GDI+

荒凉一梦 提交于 2019-12-20 01:38:41

问题


i get an

A generic error occurred in GDI+

exception when I call img.Save(path, jpegCodec, encoderParams);
here is all of the code :

    private Image img;

    private void button1_Click(object sender, EventArgs e)
    {
        this.img = Image.FromFile(@"path");

        pictureBox1.Image = img;

        if (img.Height < pictureBox1.Height && img.Width < pictureBox1.Width)
        {
            this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
        }

        Graphics g = Graphics.FromImage(img);

        Font font=new Font("Arial",16);

        SolidBrush brush = new SolidBrush(Color.Black);

        brush.Color = Color.FromArgb(255, 0, 0, 255);

        g.DrawString("myName", font, brush, img.Width - 178, img.Height-105);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Bitmap bitmap = new Bitmap(img);

        saveJpeg(@"path", bitmap, 85L);
    }

    private void saveJpeg(string path, Bitmap img, long quality)
    {
        // Encoder parameter for image quality
        EncoderParameter qualityParam =new EncoderParameter(Encoder.Quality, quality);

        // Jpeg image codec
        ImageCodecInfo jpegCodec = getEncoderInfo("image/jpeg");

        if (jpegCodec == null)
            return;

        EncoderParameters encoderParams = new EncoderParameters(1);
        encoderParams.Param[0] = qualityParam;

        //img.Save(path, jpegCodec, encoderParams);
        img.Save(path, jpegCodec, encoderParams);
    }
    private ImageCodecInfo getEncoderInfo(string mimeType)
    {
        // Get image codecs for all image formats
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

        // Find the correct image codec
        for (int i = 0; i < codecs.Length; i++)
            if (codecs[i].MimeType == mimeType)
                return codecs[i];
        return null;
    }

Would you help me?


回答1:


As long as the image object exists that was created by loading the image from the file, the file is in use. You can't save an image with the same name while the file is in use.

Instead of using Image.FromFile to load the image, open a file stream and use Image.FromStream to create the image, then close the file stream. That way the file is no longer in use, and you can replace it.




回答2:


public ActionResult CropImage(string hdnx, string hdny, string hdnw, string hdnh)
{
    string fname = "pool.jpg";
    string fpath = Path.Combine(Server.MapPath("~/images"), fname);
    Image oimg = Image.FromFile(fpath);
    Rectangle cropcords = new Rectangle(
    Convert.ToInt32(hdnx),
    Convert.ToInt32(hdny),
    Convert.ToInt32(hdnw),
    Convert.ToInt32(hdnh));
    string cfname, cfpath;
    Bitmap bitMap = new Bitmap(cropcords.Width, cropcords.Height,img.PixelFormat);

    Graphics grph = Graphics.FromImage(bitMap);
    grph.DrawImage(oimg, new Rectangle(0, 0, bitMap.Width, bitMap.Height), cropcords, GraphicsUnit.Pixel);
    cfname = "crop_" + fname;
    cfpath = Path.Combine(Server.MapPath("~/cropimages"), cfname);
    bitMap.Save(cfpath);

    return Json("success",JsonRequestBehavior.AllowGet);
}


来源:https://stackoverflow.com/questions/1781390/saving-an-image-a-generic-error-occurred-in-gdi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!