我得到了 A generic error occurred in GDI+ 我调用img.Save(path,jpegCodec,encoderParams)时的异常; 这里是所有的代码: private Image img; private void button1_Click(object sender, EventArgs e) { this.img = Image.FromFile(@"p
A generic error occurred in GDI+
我调用img.Save(path,jpegCodec,encoderParams)时的异常;
这里是所有的代码:
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; }
你能帮我吗?
只要存在通过从文件加载图像而创建的图像对象,该文件就在使用中.在使用文件时,无法保存具有相同名称的图像.不使用Image.FromFile加载图像,而是打开文件流并使用Image.FromStream创建图像,然后关闭文件流.这样文件就不再使用了,您可以替换它.