C# 如何获取“System.Drawing.Image”的文件大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/221345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the file size of a "System.Drawing.Image"
提问by Nick Allen
I am currently writing a system that stores meta data for around 140,000 ish images stored within a legacy image library that are being moved to cloud storage. I am using the following to get the jpg data...
我目前正在编写一个系统,该系统可以存储大约 140,000 个 ish 图像的元数据,这些图像存储在正在移动到云存储的旧图像库中。我正在使用以下内容来获取 jpg 数据...
System.Drawing.Image image = System.Drawing.Image.FromFile("filePath");
Im quite new to image manipulation but this is fine for getting simple values like width, height, aspect ratio etc but what I cannot work out is how to retrieve the physical file size of the jpg expressed in bytes. Any help would be much appreciated.
我对图像处理很陌生,但这对于获取宽度、高度、纵横比等简单值很好,但我无法解决的是如何检索以字节表示的 jpg 的物理文件大小。任何帮助将非常感激。
Thanks
谢谢
Final solution including an MD5 hash of the image for later comparison
最终解决方案包括图像的 MD5 哈希值以供稍后比较
System.Drawing.Image image = System.Drawing.Image.FromFile(filePath);
if (image != null)
{
int width = image.Width;
int height = image.Height;
decimal aspectRatio = width > height ? decimal.divide(width, height) : decimal.divide(height, width);
int fileSize = (int)new System.IO.FileInfo(filePath).Length;
using (System.IO.MemoryStream stream = new System.IO.MemoryStream(fileSize))
{
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
Byte[] imageBytes = stream.GetBuffer();
System.Security.Cryptography.MD5CryptoServiceProvider provider = new System.Security.Cryptography.MD5CryptoServiceProvider();
Byte[] hash = provider.ComputeHash(imageBytes);
System.Text.StringBuilder hashBuilder = new System.Text.StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
hashBuilder.Append(hash[i].ToString("X2"));
}
string md5 = hashBuilder.ToString();
}
image.Dispose();
}
采纳答案by Ilya Ryzhenkov
If you get your image directly from file, you can use the following code to get size of original file in bytes.
如果您直接从文件中获取图像,则可以使用以下代码以字节为单位获取原始文件的大小。
var fileLength = new FileInfo(filePath).Length;
If you get your image from other source, like getting one bitmap and composing it with other image, like adding watermark you will have to calculate size in run-time. You can't just use original file size, because compressing may lead to different size of output data after modification. In this case, you can use MemoryStream to save image to:
如果您从其他来源获取图像,例如获取一个位图并将其与其他图像组合,例如添加水印,您将必须在运行时计算大小。您不能只使用原始文件大小,因为压缩可能会导致修改后输出数据的大小不同。在这种情况下,您可以使用 MemoryStream 将图像保存到:
long jpegByteSize;
using (var ms = new MemoryStream(estimatedLength)) // estimatedLength can be original fileLength
{
image.Save(ms, ImageFormat.Jpeg); // save image to stream in Jpeg format
jpegByteSize = ms.Length;
}
回答by MysticSlayer
System.Drawing.Image
won't give you the file length of size. You have to use another library for that.
System.Drawing.Image
不会给你大小的文件长度。您必须为此使用另一个库。
int len = (new System.IO.FileInfo(sFullPath)).Length;
回答by Stefan Schultze
If you don't have the original file, the file size is not clear as it depends on the image format and quality. So what you'd have to do is write the image to a stream (e.g. MemoryStream) and then use the stream's size.
如果您没有原始文件,则文件大小不清楚,因为它取决于图像格式和质量。因此,您必须做的是将图像写入流(例如 MemoryStream),然后使用流的大小。