参数空异常位图保存到内存流
- 作者: 烟花寂寞丶
- 来源: 51数据库
- 2022-10-20
问题描述
我正在尝试将我的位图保存到 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.
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
