我正在尝试将我的位图保存到 MemoryStream - 这段代码有什么问题?为什么它让我argumentnullexception ?
I'm trying to save my Bitmap to MemoryStream - what wrong in this code? Why it gets me argumentnullexception ??
private void insertBarCodesToPDF(Bitmap barcode)
{
.......
MemoryStream ms = new MemoryStream();
barcode.Save(ms, System.Drawing.Imaging.ImageFormat.MemoryBmp); //<----
byte [] qwe = ms.ToArray();
.......
}
UPD: StackTraceSystem.Drawing.Image.Save(流流,ImageCodecInfo 编码器,EncoderParameters 编码器参数)在 WordTest.FormTestWord.insertBarCodesToPDF(位图条码)
UPD: StackTrace System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) in WordTest.FormTestWord.insertBarCodesToPDF(Bitmap barcode)
我相信您的问题与您尝试保存到 MemoryStream 的图像类型有关.根据此代码项目文章:动态生成图标(安全),一些 ImageFormat类型没有必要的编码器来允许保存功能保存为该类型.
I believe that your problem is related to the type of image you are trying to save to the MemoryStream as. According to this Code Project article: Dynamically Generating Icons (safely), some of the ImageFormat types do not have the necessary encoder to allow the Save function to save as that type.
我运行以下命令来确定哪些类型有效,哪些无效:
I ran the following to determine which types did and didn't work:
System.Drawing.Bitmap b = new Bitmap(10, 10);
foreach (ImageFormat format in new ImageFormat[]{
ImageFormat.Bmp,
ImageFormat.Emf,
ImageFormat.Exif,
ImageFormat.Gif,
ImageFormat.Icon,
ImageFormat.Jpeg,
ImageFormat.MemoryBmp,
ImageFormat.Png,
ImageFormat.Tiff,
ImageFormat.Wmf})
{
Console.Write("Trying {0}:", format);
MemoryStream ms = new MemoryStream();
bool success = true;
try
{
b.Save(ms, format);
}
catch (Exception)
{
success = false;
}
Console.WriteLine(" {0}", (success ? "works" : "fails"));
}
结果如下:
Trying Bmp: works
Trying Emf: fails
Trying Exif: fails
Trying Gif: works
Trying Icon: fails
Trying Jpeg: works
Trying MemoryBMP: fails
Trying Png: works
Trying Tiff: works
Trying Wmf: fails
有一个 Microsoft KB 文章,其中指出某些 ImageFormat类型是只读的.
There is a Microsoft KB Article which states that some of the ImageFormat types are read-only.
这篇关于参数空异常位图保存到内存流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!