本文介绍了如何修复GDI +中创建条形码图像时发生的一般错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在生成条形码图像及其上的数字,并打印出来,如果我创建了另一个具有不同数字或相同数字的条形码,则会导致错误i'm generating a barcode image along with a number on it,and print it, if i created another barcode with a different number or same number, it will cause an error in bm.Save("temp.gif");这是我的代码 here's my code private void btnSave_Click(object sender, EventArgs e) { Barcode128 code = new Barcode128(); RandomGenerator generator = new RandomGenerator(); try { int rand = generator.RandomNumber(0, 1234567890); label1.Text = rand.ToString(); string barcode = rand.ToString(); code.Size = 10F; code.CodeType = Barcode.CODE128; code.ChecksumText = false; code.GenerateChecksum = false; code.StartStopText = false; if (barcode.Length == 19) { barcode = "0" + barcode; } code.Code = barcode; Bitmap bm = new Bitmap(code.CreateDrawingImage(Color.Black, Color.White)); bm.Save("temp.gif"); picBarcode1.Load("temp.gif"); using (FolderBrowserDialog fdb = new FolderBrowserDialog()) { fdb.Description = "fdf"; if (fdb.ShowDialog() == DialogResult.OK) { picBarcode1.Image.Save(@fdb.SelectedPath + "\\Image" + DateTime.Now.ToString("ddMMyyyyHHmmss") + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); MessageBox.Show("Barcode Image saved Successfully. Path : " + fdb.SelectedPath); } } } catch (Exception) { throw; } } 我尝试了什么: i尝试制作一个后退按钮,它完美无缺,但我不想一次又一次地来回。What I have tried:i tried to make a back button and it works perfectly but i don't want to go back and forth again and again.推荐答案 你必须(看看/与编写的人交谈)你的Barcode128课程 - 我们不知道它是做什么或如何使用它,所以我们真的,真的无法帮助你。 如果我不得不猜测,我怀疑 code.CreateDrawingImage 正在抛出异常(这需要查看类方法我们没有)或它返回非图像数据,如文本或空字节数组。 使用调试器确切地找出它发生的位置,以及究竟是什么被发送到Bitmap构造函数 - 拆分行以便存储以便快速查看是最简单的方法: You'll have to (look at / talk to the people who wrote) your Barcode128 class - we have no idea what is does or how to use it, so we really, really can't help you. If I had to guess, I'd suspect that either code.CreateDrawingImage is throwing the exception (and that needs looking into the class methods which we don't have) or it's returning non-image data such as text or an empty byte array.Use the debugger to find out exactly where it happens, and exactly what is being sent to the Bitmap constructor - split the line so it's stored for a quick look is the easiest way:var returned = code.CreateDrawingImage(Color.Black, Color.White);Bitmap bm = new Bitmap(returned); 抱歉,我们无法为您做任何事情!Sorry, but we can't do any of that for you! 当您收到错误时,请始终说明它出现在哪一行。事实上,你不认为引发错误的行与解决问题有关,这有点令人担忧。 如果我不得不猜测(我们有猜测,因为我们不知道哪一行抛出错误)这是因为你保存到temp.gif并且该文件被锁定,因为你没有丢弃以前的Bitmap所以文件仍在使用中 When you get an error always say what line it occurs on. The fact that you don't think the line that throws the error is relevant to solving your problem is a little worrying.If I had to guess (and we have to guess as we don't know what line throws the error) it would be because you are saving to temp.gif and that file is locked because you haven't disposed the previous Bitmap so the file is still in useBitmap bm = new Bitmap(code.CreateDrawingImage(Color.Black, Color.White));bm.Save("temp.gif");bm.Dispose(); // dispose the Bitmap when you're finished with it 这篇关于如何修复GDI +中创建条形码图像时发生的一般错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-27 09:14