固定和QUOT;而在GDI + QUOT发生一般错误;同时产生一个吧code而在、错误、发生、QUOT

2023-09-04 00:07:35 作者:心若向阳、便是晴天

我写在C#中我可以生成棒codeS为位图,可以向他们展示在一个PictureBox(WindowsForms)酒吧code发电机。在另一方面,我也救不了我的吧code为GIF或JPEG文件。我的吧code是一个位图文件,这是我的code

I am writing a barcode generator in C# I can generate barcodes as bitmaps and can show them in a Picturebox(WindowsForms). On the other hand I could not save my barcode as a gif or jpeg file. My barcode is a bitmap file and here is my code

Bitmap barCode = CreateBarCode("*"+txtBarCodeStr.Text+"*");
barCode.Save(@"C:\barcode.gif", ImageFormat.gif);
picBox.Image = barCode;            
MessageBox.Show("Created");

我得到下面的异常; GDI +中发生一般性错误。

I get the following exception; A generic error occurred in GDI+.

推荐答案

注:标题是误导性的,因为没有任何问题与建立CS描述吧$ C $,取而代之的则是关于保存图像/位图。我只能假设Puthiyedath先生,赏金提供者,是有同样的错误,因为没有进一步的信息提供。

Note: the title is misleading since no problem relating to creating BarCodes is described, instead it is about saving images/bitmaps. I can only assume Mr Puthiyedath, the bounty provider, is having the same error since no further information is provided.

一般性错误问题通常是指以下两种情况之一: A)您正在试图写它已经有一个位图图像就可以打开一个文件。从的 MSDN :

The generic error occurred in GDI+ problem usually means one of two things: A) You are trying to write to a file which already has a bitmap image open on it. From MSDN:

保存图像这是从不允许建造并抛出异常。

Saving the image to the same file it was constructed from is not allowed and throws an exception.

所以,如果你有一个位图从文件创建的应用程序在其他地方,你可以不回保存到同一个文件,直到位设置。显而易见的解决方案是创建一个新的位图,保存它。

So, if you have a bitmap elsewhere in the app created from a file, you cannot save back to that same file until the bitmap is disposed. The obvious solution is to create a new bitmap and save it.

B)您可能没有访问该文件夹/文件

这是,因为执行文件IO在启动驱动器的根文件夹中的文件(如样品code的OP),可能会被禁止根据更可能出现的问题操作系统,安全设置,权限等等等等等。

This is the more likely problem because performing File IO to a file in the root folder of the boot drive (as in the sample code by the OP), may be disallowed depending on the OS, security settings, permissions etc etc etc.

在code以下,如果我尝试保存到C:\ ziggybar code.gif,而不是 \温度文件夹,的什么也没有发生的。没有异常,但没有图像要么是创建。在不同条件下的异常可能会导致,但往往这些似乎被报告为一般错误。 解决方法:不要使用root文件夹中的文件。 Users \用户名\ ... 是为这个目的。

In the code below, if I try to save to "C:\ziggybarcode.gif" rather than the\Temp folder, nothing happens. There is no exception but no image is created either. Under different conditions an exception can result, but often these seem to be reported as the Generic Error. Solution: Don't use the root folder for files. Users\USERNAME\... is intended for just this purpose.

有时会出现,而且可能会感兴趣的另外一个例外,就是内存溢出异常。这通常意味着没有与该图像格式中的问题。我有一对夫妇是做了很多工作,对图像,发现这是有问题的跟踪格式或假设的,一个图像可以保存为特定格式的任何给定的机器上的应用程序。酒吧code发电机可能有类似的问题。

One other exception which sometimes comes up, and may be of interest, is Out Of Memory Exception. This usually means there is a problem with the image format. I have a couple of apps that do a lot of work on images and found it was problematic tracking the format or assuming that an image can be saved to a particular format on any given machine. A BarCode generator might have a similar issue.

对于简单的应用程序或那些只处理一个单一的标准格式,简单的 Bitmap.Save 方法往往会被罚款:

For simple apps or ones which only deal with a single, standard format, the simple Bitmap.Save method will often be fine:

myBmp.Save(@"C:\Temp\mybarcode.gif", ImageFormat.Gif);

但是,当你的应用程序是杂耍不同的格式,它可能是最好明确EN code中的形象:

But when your app is juggling different formats, it might be better to explicitly encode the image:

public static bool SaveImageFile(string fileName, ImageFormat format, Image img, 
              Int64 quality)
{
    ImageCodecInfo codec = GetEncoder(format);
    EncoderParameter qParam = new EncoderParameter(Encoder.Quality, quality);

    EncoderParameters encoderParams = new EncoderParameters(1);
    encoderParams.Param[0] = qParam;
    try
    {
        img.Save(fileName, codec, encoderParams);
        return true;
    }
    catch
    {
        return false;
    }
}

private static ImageCodecInfo GetEncoder(ImageFormat format)
{
    var codecs = ImageCodecInfo.GetImageDecoders();
    foreach (var codec in codecs)
    {
        if (codec.FormatID == format.Guid)
        {
            return codec;
        }
    }
    return null;
}

把它们放在一个DLL或使他们的扩展,并成为他们同样简单一起工作:

Put them in a DLL or make them extensions, and they become equally simple to work with:

SaveImageFile(@"c:\Temp\myBarCode.gif",
                  ImageFormat.Gif, myBmp, 100);

特别是对于JPG格式工作时,就变得特别容易管理的COM pression /质量参数。我不是说这是preferred方法,只是说的 I 的有通过显式编码的图像在复杂的应用程序少得多的问题。

Particularly when working with JPGs, it becomes especially easy to manage the compression/quality parameter. I am not saying this is the preferred method, just that I have had far fewer problems in complex apps by explicitly encoding images.

测试code ,以创建一个吧code GIF:

Test code to create a BarCode GIF:

private void button1_Click(object sender, EventArgs e)
{
    Bitmap bmp = CreateBarcode("Ziggy12345");
    this.pb.Image = bmp;

    try { 
    // save to root of boot drive
    // does nothing on my system: no Error, No File
     bmp.Save(@"C:\zBarCodeDIRECT.gif", ImageFormat.Gif);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }

    // save BMP to file
    SaveImageFile(@"c:\Temp\zBarCodeBITMAP.gif",
                      ImageFormat.Gif, bmp, 100);

    // save PicBox image
    SaveImageFile(@"c:\Temp\zBarCodePICBOX.gif",
                ImageFormat.Gif, this.pb.Image, 100);

    // Try to save to same file while a bmp is open on it
    Bitmap myBmp = (Bitmap)Image.FromFile(@"c:\Temp\zBarCodeBITMAP.gif");
    // A generic Error in GDI
    myBmp.Save(@"c:\Temp\zBarCodeBITMAP.gif", ImageFormat.Gif);
}

最后一个失败的通用GDI误差,即使它的看起来的它应该是一个IO或UnAuthorizedAccess相关的异常。

The last one fails with the Generic GDI error even though it seems like it should be an IO or UnAuthorizedAccess related exception.

输出:

第一个和最后一个图像不除上述原因和code注释说明。

The first and last images do not save for the reasons explained above and in code comments.