C# 如何获取“文件”的文件大小;“系统.图纸.图像”;
我目前正在编写一个系统,该系统存储了大约140000个ish图像的元数据,这些图像存储在一个将被移动到云存储的遗留图像库中。我正在使用以下内容获取jpg数据C# 如何获取“文件”的文件大小;“系统.图纸.图像”;,c#,image,image-processing,image-manipulation,C#,Image,Image Processing,Image Manipulation,我目前正在编写一个系统,该系统存储了大约140000个ish图像的元数据,这些图像存储在一个将被移动到云存储的遗留图像库中。我正在使用以下内容获取jpg数据 System.Drawing.Image image = System.Drawing.Image.FromFile("filePath"); 我对图像处理非常陌生,但这对于获取诸如宽度、高度、纵横比等简单值来说很好,但我无法解决的是如何检索jpg的物理文件大小(以字节表示)。任何帮助都将不胜感激 谢谢 最终解决方案包括图像的MD5散列,
System.Drawing.Image image = System.Drawing.Image.FromFile("filePath");
我对图像处理非常陌生,但这对于获取诸如宽度、高度、纵横比等简单值来说很好,但我无法解决的是如何检索jpg的物理文件大小(以字节表示)。任何帮助都将不胜感激
谢谢
最终解决方案包括图像的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();
}
System.Drawing.Image=System.Drawing.Image.FromFile(文件路径);
如果(图像!=null)
{
int-width=image.width;
int height=image.height;
decimal aspectRatio=宽度>高度?十进制.除法(宽度,高度):十进制.除法(高度,宽度);
int fileSize=(int)new System.IO.FileInfo(filePath.Length;
使用(System.IO.MemoryStream stream=new System.IO.MemoryStream(文件大小))
{
保存(流、系统、绘图、成像、图像格式、Jpeg);
Byte[]imageBytes=stream.GetBuffer();
System.Security.Cryptography.MD5CryptoServiceProvider provider=新的System.Security.Cryptography.MD5CryptoServiceProvider();
Byte[]hash=provider.ComputeHash(imageBytes);
System.Text.StringBuilder hashBuilder=新的System.Text.StringBuilder();
for(int i=0;i
如果您直接从文件中获取图像,可以使用以下代码获取原始文件的大小(以字节为单位)
var fileLength = new FileInfo(filePath).Length;
如果您从其他来源获取图像,如获取一个位图并将其与其他图像组合,如添加水印,则必须在运行时计算大小。不能只使用原始文件大小,因为压缩可能会导致修改后输出数据的大小不同。在这种情况下,您可以使用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;
}
System.Drawing.Image
不会给出文件大小的长度。你必须使用另一个图书馆
int len = (new System.IO.FileInfo(sFullPath)).Length;
如果没有原始文件,则文件大小不清楚,因为它取决于图像格式和质量。因此,您需要做的是将图像写入流(例如MemoryStream),然后使用流的大小