如何在.net中不使用位图或图形对象来获取图像尺寸

发布于 2024-11-15 20:43:09 字数 1238 浏览 2 评论 0原文

我想从 Visual C# 创建 SQL CLR 集成函数,现在我的要求是用户将文件夹路径作为参数传递,并且该函数应该从文件夹中获取所有图像文件,并获取其基本属性,如文件大小、尺寸等..但似乎SQL项目不支持System.Drawing命名空间...因为我在正常项目中创建了相同的函数,它工作得很好,因为我能够使用System.Drawing命名空间,但在这里我不能使用System.Drawing命名空间..所以有没有其他方法来获取图像尺寸...

下面是我在正常项目中使用的代码。

public DataTable InsertFile(string FolderPath)  
{   
    DataTable dt = new DataTable();  
    DataColumn[] col = new DataColumn[] { new DataColumn("FileName",   typeof(System.String)), new DataColumn("FileSize", typeof(System.Int32)), new   DataColumn("FilePath", typeof(System.String)), new DataColumn("Width",   typeof(System.Int32)), new DataColumn("Height", typeof(System.Int32)) };  
    dt.Columns.AddRange(col);  
    FileInfo info= null;  
    Bitmap bmp = null;  
    foreach (String s in Directory.GetFiles(FolderPath, "*.jpg"))  
    {  
        info = new FileInfo(s);  
        bmp = new Bitmap(s);  
        DataRow dr = dt.NewRow();  
        dr["FileName"] = Path.GetFileName(s); 
        dr["FileSize"] = info.Length / 1024;    
        dr["FilePath"] = s;  
        dr["Width"] = bmp.Width;  
        dr["Height"] = bmp.Height;  
        dt.Rows.Add(dr);  
    }  
    return dt;  
}  

有谁知道如何在不使用 System.Drawing 命名空间的情况下获取图像尺寸。

i want to create SQL CLR integrated function from Visual C#, now my requirement is user will pass a folder path as a paramter, and the function should get all the image file from the the folder, and get its basic property like FileSize, dimension etc.. but it seems SQL project does not supports System.Drawing Namespace... as i created the same function in normal project it worked fine, as i was able to use System.Drawing Namespace, but here i cannot use, System.Drawing Namespace.. so is there any other way to get the image dimension...

below is the code i have used in my normal project.

public DataTable InsertFile(string FolderPath)  
{   
    DataTable dt = new DataTable();  
    DataColumn[] col = new DataColumn[] { new DataColumn("FileName",   typeof(System.String)), new DataColumn("FileSize", typeof(System.Int32)), new   DataColumn("FilePath", typeof(System.String)), new DataColumn("Width",   typeof(System.Int32)), new DataColumn("Height", typeof(System.Int32)) };  
    dt.Columns.AddRange(col);  
    FileInfo info= null;  
    Bitmap bmp = null;  
    foreach (String s in Directory.GetFiles(FolderPath, "*.jpg"))  
    {  
        info = new FileInfo(s);  
        bmp = new Bitmap(s);  
        DataRow dr = dt.NewRow();  
        dr["FileName"] = Path.GetFileName(s); 
        dr["FileSize"] = info.Length / 1024;    
        dr["FilePath"] = s;  
        dr["Width"] = bmp.Width;  
        dr["Height"] = bmp.Height;  
        dt.Rows.Add(dr);  
    }  
    return dt;  
}  

does anyone have any idea how to get image dimension without using System.Drawing Namespace.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜光 2024-11-22 20:43:09

哇以前从未见过有人尝试过这个,但如果不允许在 SQL 项目中使用绘图,请尝试读取这样的标题信息 http://www.codeproject.com/KB/cs/ReadingImageHeaders.aspx

编辑包含代码,并进行更改以删除对 Size 的依赖。

while (binaryReader.ReadByte() == 0xff)
        {
            byte marker = binaryReader.ReadByte();
            ushort chunkLength = binaryReader.ReadLittleEndianInt16();

            if (marker == 0xc0)
            {
                binaryReader.ReadByte();

                int height = binaryReader.ReadLittleEndianInt16();
                int width = binaryReader.ReadLittleEndianInt16();
                return new int[] { width, height };
            }

            binaryReader.ReadBytes(chunkLength - 2);
        }

wow never seen anyone try this before, but if using Drawing in a SQL project isn't allowed try reading the header info like this http://www.codeproject.com/KB/cs/ReadingImageHeaders.aspx

Edit included the code, with the change to remove the dependency on Size.

while (binaryReader.ReadByte() == 0xff)
        {
            byte marker = binaryReader.ReadByte();
            ushort chunkLength = binaryReader.ReadLittleEndianInt16();

            if (marker == 0xc0)
            {
                binaryReader.ReadByte();

                int height = binaryReader.ReadLittleEndianInt16();
                int width = binaryReader.ReadLittleEndianInt16();
                return new int[] { width, height };
            }

            binaryReader.ReadBytes(chunkLength - 2);
        }
┈┾☆殇 2024-11-22 20:43:09

Image 对象对您来说更好吗?

System.Drawing.Image forSize = System.Drawing.Image.FromFile(s);
dr["Width"] = forSize.Width;

等等。

有更好的或同样的问题吗?

Is the Image object any better for you?

System.Drawing.Image forSize = System.Drawing.Image.FromFile(s);
dr["Width"] = forSize.Width;

and so forth.

That any better or same problem?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文