- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我已经构建了一个程序,允许我通过 System.Image.Drawing
插入图像的评论和标题,所以现在,我无法尝试用一个覆盖现有的 Jpeg 文件添加了评论和标题,但我在删除文件时遇到错误,所以我不确定该怎么做,因为我已经尝试处理该文件,但在那种情况下我无法保存它,因为我处理得太早了,但我无法保存它,因为现有的文件名没有被删除,所以我现在有点卡在中间。
这是我的代码:
public string EditComment(string OriginalFilepath, string newFilename)
{
image = System.Drawing.Image.FromFile(OriginalFilepath);
PropertyItem propItem = image.PropertyItems[0];
using (var file = System.Drawing.Image.FromFile(OriginalFilepath))
{
propItem.Id = 0x9286; // this is the id for 'UserComment'
propItem.Type = 2;
propItem.Value = System.Text.Encoding.UTF8.GetBytes("HelloWorld\0");
propItem.Len = propItem.Value.Length;
file.SetPropertyItem(propItem);
PropertyItem propItem1 = file.PropertyItems[file.PropertyItems.Count() - 1];
file.Dispose();
image.Dispose();
string filepath = Filepath;
if (File.Exists(@"C:\Desktop\Metadata"))
{
System.IO.File.Delete(@"C:\Desktop\Metadata");
}
string newFilepath = filepath + newFilename;
file.Save(newFilepath, ImageFormat.Jpeg);//error appears here
return filepath;
}
}
显示的错误是:
An exception of type 'System.ArgumentException' occurred in System.Drawing.dll but was not handled in user code
Additional information: Parameter is not valid.
最佳答案
问题是从文件中打开图像会锁定文件。您可以通过将文件读入字节数组,从中创建内存流,然后从该流中打开图像来解决这个问题:
public string EditComment(string originalFilepath, string newFilename)
{
Byte[] bytes = File.ReadAllBytes(originalFilepath);
using (MemoryStream stream = new MemoryStream(bytes))
using (Bitmap image = new Bitmap(stream))
{
PropertyItem propItem = image.PropertyItems[0];
// Processing code
propItem.Id = 0x9286; // this is the id for 'UserComment'
propItem.Type = 2;
propItem.Value = System.Text.Encoding.UTF8.GetBytes("HelloWorld\0");
propItem.Len = propItem.Value.Length;
image.SetPropertyItem(propItem);
// Not sure where your FilePath comes from but I'm just
// putting it in the same folder with the new name.
String newFilepath;
if (newFilename == null)
newFilepath = originalFilePath;
else
newFilepath = Path.Combine(Path.GetDirectory(originalFilepath), newFilename);
image.Save(newFilepath, ImageFormat.Jpeg);
return newFilepath;
}
}
确保您不像在测试代码中那样将图像对象放置在using
block 中。不仅 using
block 完全存在,因此您不必手动处理,而且将不再存在于内存中的图像保存到磁盘也相当困难。同样,您似乎从文件中打开图像两次。我只是假设所有这些都是试图解决问题的实验,但一定要确保清理掉它们。
打开图像时要记住的基本规则是:
Image
对象 will lock the file during the life cycle of the image object ,防止文件被覆盖或删除,直到图像被处理。Image
对象 will need the stream to remain open for the entire life cycle of the image object .与文件不同,没有任何主动强制这样做,但在流关闭后,图像在保存、克隆或以其他方式操作时会出错。与某些人的看法相反,对图像对象的基本 .Clone()
调用将不会改变此行为。克隆的对象仍将保留对原始源的引用。
请注意,如果您确实想要一个未包含在 using
block 中的可用图像对象,you can use LockBits
and Marshal.Copy
to copy the byte data of the image object into a new image with the same dimensions and the same PixelFormat
,有效地制作了原始图像的完整数据克隆。 (注意:我认为这不适用于动画 GIF 文件)完成之后,您可以安全地处理原始文件并只使用新的干净克隆版本。
还有一些其他解决方法可以实际输出图像,但我见过的大多数都不是最佳方法。以下是锁定问题的两个最常见的其他有效解决方法:
Bitmap(Image image)
构造函数从文件加载的图像创建一个新的 Bitmap
。这个新对象将不具有指向该文件的链接,让您可以自由处置锁定文件的对象。这非常有效,但它会将图像的颜色深度更改为 32 位 ARGB,这可能不是您想要的。不过,如果您只需要在 UI 上显示图像,这是一个很好的解决方案。MemoryStream
,但不是在 using
block 中,根据需要保持流打开。让流打开对我来说并不是一个好主意。尽管有些人说过,由于 MemoryStream
仅由一个简单的数组支持,而不是某些外部资源,垃圾收集器显然可以很好地处理这种情况...我也看到有人使用 System.Drawing.ImageConverter
从字节转换,但是 I looked into the internals of that process , 它所做的实际上与这里的最后一个方法相同,它使内存流保持打开状态。
关于c# - 覆盖现有的 Jpeg 文件/用编辑后的 JPEG 文件替换现有的 JPEG 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48579643/
在 Google 和许多 DICOM 章节中可以轻松找到有关 JPEG-LS 的信息。 但是,也有提到 JPEG-LL 的链接/页面/阅读资料。但是,我更深入地研究了 DICOM 标准,没有一章提到过
我有一个关于 DICOM 标准和 libjpeg 库的问题。在 DICOM 标准中,除其他外,还有传输语法: JPEG Lossless, Nonhierarchical, First- Order
APP0 到 APP15 标记每个只支持 65535 字节(我从 libjpeg.doc 中读到的)。如果要在 jpeg 文件中保存更大的数据 block 怎么办? 最佳答案 没有限制使用多个相同类型
我正在尝试构建一个上传器,它分两步上传渐进式文件: 上传最小字节数以创建缩略图 (0-10%) 上传缩略图的其余字节。 (11%-100%) 我想这样做是为了让缩略图更早可用,而不必上传单独的缩略图。
所以,假设我有一个大版本的图像,它只显示为缩略图。是否可以通过使用渐进式 jpeg 来避免为缩略图创建单独的文件,在达到一定数量的扫描时停止加载,并且仅在用户选择完全打开它时继续加载? 如果是这样,如
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 去年关闭。 Improve this
很难说出这里问的是什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或言辞激烈,无法以目前的形式合理回答。如需帮助澄清此问题以便可以重新打开,visit the help center . 9年前关闭
我想从 jpeg 中提取缩略图,无需任何外部库。我的意思是这并不太困难,因为我需要知道缩略图在文件中的开始位置和结束位置,然后简单地剪切它。我研究了许多文档(即: http://www.media.m
当 decoding entropy encoded DC values in JPEG (或 entropy encoded prediction differences in lossless J
问题是:如何区分两个文件?一个用 JPEG 编码,另一个用 JPEG2000 编码。 我需要特定于格式的文件读/写函数,我无法在不读取的情况下找到文件编码。JPEG 现在工作正常,但 JPEG fun
是否有结束 exif/end-of-xmp/end-of-iptc/start-of-data 标记,我可以用它来获取 jpg/jpeg(和其他图像格式)的数据部分的校验和? 最佳答案 我认为这个问题
不久前我收到一封电子邮件,其中包含图像附件。从那时起,hotmail 似乎已停止为我托管图像,因为当我打开邮件时,图像不再可用。 但是,消息源仍然完好无损,如果我没记错的话,消息源 - 以文本形式 -
我有一个提供高质量 MJPEG 的网络摄像头。 我需要通过网络发送小的、低质量的 JPEG。我的硬件是树莓派(700MHz ARM)。我希望代码使用尽可能少的 CPU 能力,并尽可能少地增加延迟。我可
有一个question with the same title但不幸的是它对我没有帮助。 我正在尝试解析 SOS 标记的数据。我能找到的所有文档都说在标记( 0xFFDA )之后是一个两字节的数字,它
每个人! 我处理来自 IP 摄像机的视频,并编写了基于解压缩视频分析的运动检测算法。但我真的更快。我找到了几篇关于压缩域分析的论文,但没有找到任何实现。 谁能推荐我一些代码? 找到的 Material
我在一个网站上工作,该网站涉及显示来自各种在线零售商的大量产品图片。由于大部分页面权重都在图像中,因此我认为值得研究一下减少文件大小的技术。 图像已经是 JPEG。我知道 PNG 有很多多余的东西,可
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我有一个网络画廊,我在其中显示用户上传的文件大小和分辨率各不相同的图像。目前所有图像都是基线。所以我想知道如果我将它们转换为逐行图像是否真的会产生重大影响。使用渐进式图像的优点和缺点是什么。 最佳答案
我在许多链接和网站上保持头脑,但未能得到答案。我不想问这个,我知道 JPEG 压缩,它只制作压缩图像。甚至 Motion JPEG 也会制作压缩图像 I 帧。我的问题是有什么区别。我正在为需要发送视频
我是一名优秀的程序员,十分优秀!