संपूर्ण फ़ाइल को पढ़े बिना छवि आयाम प्राप्त करना


104

क्या एक छवि (jpg, png, ...) के आयाम प्राप्त करने का एक सस्ता तरीका है? अधिमानतः, मैं केवल मानक वर्ग पुस्तकालय (होस्टिंग प्रतिबंधों के कारण) का उपयोग करके इसे प्राप्त करना चाहूंगा। मुझे पता है कि छवि हेडर को पढ़ना और इसे स्वयं पार्स करना अपेक्षाकृत आसान होना चाहिए, लेकिन ऐसा लगता है कि ऐसा कुछ पहले से ही होना चाहिए। साथ ही, मैंने सत्यापित किया है कि निम्नलिखित कोड पूरी छवि को पढ़ता है (जो मुझे नहीं चाहिए):

using System;
using System.Drawing;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Image img = new Bitmap("test.png");
            System.Console.WriteLine(img.Width + " x " + img.Height);
        }
    }
}

यह मदद करेगा यदि आप उचित प्रश्न में थोड़ा अधिक विशिष्ट थे। टैग ने मुझे .net और c # बताया है, और आप मानक पुस्तकालय चाहते हैं, लेकिन इन होस्टिंग प्रतिबंधों का आप क्या उल्लेख करते हैं?
wnoise

यदि आपके पास System.Windows.Media.Imaming नाम स्थान (WPF में) तक पहुँच है, तो यह SO प्रश्न देखें: stackoverflow.com/questions/784734/…
चार्ली

जवाबों:


106

हमेशा की तरह आपका सबसे अच्छा दांव एक अच्छी तरह से जांचा गया पुस्तकालय खोजना है। हालाँकि, आपने कहा था कि यह कठिन है, इसलिए यहाँ कुछ मोटे तौर पर अनुपयोगी कोड है जो उचित संख्या में मामलों के लिए काम करना चाहिए:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;

namespace ImageDimensions
{
    public static class ImageHelper
    {
        const string errorMessage = "Could not recognize image format.";

        private static Dictionary<byte[], Func<BinaryReader, Size>> imageFormatDecoders = new Dictionary<byte[], Func<BinaryReader, Size>>()
        {
            { new byte[]{ 0x42, 0x4D }, DecodeBitmap},
            { new byte[]{ 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 }, DecodeGif },
            { new byte[]{ 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }, DecodeGif },
            { new byte[]{ 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, DecodePng },
            { new byte[]{ 0xff, 0xd8 }, DecodeJfif },
        };

        /// <summary>
        /// Gets the dimensions of an image.
        /// </summary>
        /// <param name="path">The path of the image to get the dimensions of.</param>
        /// <returns>The dimensions of the specified image.</returns>
        /// <exception cref="ArgumentException">The image was of an unrecognized format.</exception>
        public static Size GetDimensions(string path)
        {
            using (BinaryReader binaryReader = new BinaryReader(File.OpenRead(path)))
            {
                try
                {
                    return GetDimensions(binaryReader);
                }
                catch (ArgumentException e)
                {
                    if (e.Message.StartsWith(errorMessage))
                    {
                        throw new ArgumentException(errorMessage, "path", e);
                    }
                    else
                    {
                        throw e;
                    }
                }
            }
        }

        /// <summary>
        /// Gets the dimensions of an image.
        /// </summary>
        /// <param name="path">The path of the image to get the dimensions of.</param>
        /// <returns>The dimensions of the specified image.</returns>
        /// <exception cref="ArgumentException">The image was of an unrecognized format.</exception>    
        public static Size GetDimensions(BinaryReader binaryReader)
        {
            int maxMagicBytesLength = imageFormatDecoders.Keys.OrderByDescending(x => x.Length).First().Length;

            byte[] magicBytes = new byte[maxMagicBytesLength];

            for (int i = 0; i < maxMagicBytesLength; i += 1)
            {
                magicBytes[i] = binaryReader.ReadByte();

                foreach(var kvPair in imageFormatDecoders)
                {
                    if (magicBytes.StartsWith(kvPair.Key))
                    {
                        return kvPair.Value(binaryReader);
                    }
                }
            }

            throw new ArgumentException(errorMessage, "binaryReader");
        }

        private static bool StartsWith(this byte[] thisBytes, byte[] thatBytes)
        {
            for(int i = 0; i < thatBytes.Length; i+= 1)
            {
                if (thisBytes[i] != thatBytes[i])
                {
                    return false;
                }
            }
            return true;
        }

        private static short ReadLittleEndianInt16(this BinaryReader binaryReader)
        {
            byte[] bytes = new byte[sizeof(short)];
            for (int i = 0; i < sizeof(short); i += 1)
            {
                bytes[sizeof(short) - 1 - i] = binaryReader.ReadByte();
            }
            return BitConverter.ToInt16(bytes, 0);
        }

        private static int ReadLittleEndianInt32(this BinaryReader binaryReader)
        {
            byte[] bytes = new byte[sizeof(int)];
            for (int i = 0; i < sizeof(int); i += 1)
            {
                bytes[sizeof(int) - 1 - i] = binaryReader.ReadByte();
            }
            return BitConverter.ToInt32(bytes, 0);
        }

        private static Size DecodeBitmap(BinaryReader binaryReader)
        {
            binaryReader.ReadBytes(16);
            int width = binaryReader.ReadInt32();
            int height = binaryReader.ReadInt32();
            return new Size(width, height);
        }

        private static Size DecodeGif(BinaryReader binaryReader)
        {
            int width = binaryReader.ReadInt16();
            int height = binaryReader.ReadInt16();
            return new Size(width, height);
        }

        private static Size DecodePng(BinaryReader binaryReader)
        {
            binaryReader.ReadBytes(8);
            int width = binaryReader.ReadLittleEndianInt32();
            int height = binaryReader.ReadLittleEndianInt32();
            return new Size(width, height);
        }

        private static Size DecodeJfif(BinaryReader binaryReader)
        {
            while (binaryReader.ReadByte() == 0xff)
            {
                byte marker = binaryReader.ReadByte();
                short chunkLength = binaryReader.ReadLittleEndianInt16();

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

                    int height = binaryReader.ReadLittleEndianInt16();
                    int width = binaryReader.ReadLittleEndianInt16();
                    return new Size(width, height);
                }

                binaryReader.ReadBytes(chunkLength - 2);
            }

            throw new ArgumentException(errorMessage);
        }
    }
}

उम्मीद है कि कोड काफी स्पष्ट है। एक नया फ़ाइल प्रारूप जोड़ने के लिए आप इसे imageFormatDecoders"मैजिक बिट्स" की एक सरणी होने की कुंजी के साथ जोड़ते हैं , जो दिए गए प्रारूप की प्रत्येक फ़ाइल की शुरुआत में दिखाई देते हैं और मान एक फ़ंक्शन होता है जो स्ट्रीम से आकार को निकालता है। अधिकांश प्रारूप सरल हैं, केवल वास्तविक बदबूदार jpeg है।


6
सहमत, जेपीईजी बेकार है। Btw - उन लोगों के लिए एक नोट जो भविष्य में इस कोड का उपयोग करना चाहते हैं: यह वास्तव में अप्रयुक्त है। मैं इसके साथ ठीक कंघी से गुजरा हूं, और यहां मैंने जो पाया है: बीएमपी प्रारूप में एक और (प्राचीन) हेडर भिन्नता है जहां आयाम 16-बिट हैं; प्लस ऊँचाई नकारात्मक हो सकती है (फिर संकेत को छोड़ दें)। JPEG के लिए - 0xC0 केवल हेडर नहीं है। मूल रूप से 0xC4 और 0xCC को छोड़कर सभी 0xC0 से 0xCF के वैध हेडर हैं (आप इन्हें इंटरलेज्ड JPGs में आसानी से प्राप्त कर सकते हैं)। और, चीजों को और अधिक मजेदार बनाने के लिए, ऊंचाई 0xDC ब्लॉक में 0 हो सकती है और बाद में निर्दिष्ट की जा सकती है। देखें w3.org/Graphics/JPEG/itu-t81.pdf
Vilx-

मूल (मार्कर == 0xC0) का विस्तार करने के लिए ऊपर दी गई डिकोडेजिफ विधि को ट्वीक किया गया था ताकि 0xC1 और 0xC2 को भी स्वीकार किया जा सके। ये अन्य स्टार्ट-ऑफ-फ्रेम हेडर SOF1 और SOF2 एक ही बाइट पोजिशन में चौड़ाई / ऊंचाई को एनकोड करते हैं। SOF2 काफी सामान्य है।
रायन बार्टन 20

4
मानक चेतावनी: आपको कभी नहीं लिखना चाहिए, throw e;बल्कि throw;इसके बजाय। दूसरे पर आपकी एक्सएमएल GetDimensionspathbinaryReader
डॉक्स

1
इसके अलावा, ऐसा लगता है कि यह कोड EXIF ​​/ TIFF प्रारूप में एन्कोडेड JPEGs को स्वीकार नहीं करता है जो कई डिजिटल कैमरों द्वारा आउटपुट है। यह केवल JFIF का समर्थन करता है।
cwills

2
System.Drawing.Image.FromStream (स्ट्रीम, गलत, गलत) पूरी छवि को लोड किए बिना आपको आयाम देगा, और यह किसी भी छवि पर काम करता है। नेट लोड कर सकता है। यह गन्दा और अधूरा समाधान इतने सारे उभार क्यों है यह समझ से परे है।
२०:२

25
using (FileStream file = new FileStream(this.ImageFileName, FileMode.Open, FileAccess.Read))
{
    using (Image tif = Image.FromStream(stream: file, 
                                        useEmbeddedColorManagement: false,
                                        validateImageData: false))
    {
        float width = tif.PhysicalDimension.Width;
        float height = tif.PhysicalDimension.Height;
        float hresolution = tif.HorizontalResolution;
        float vresolution = tif.VerticalResolution;
     }
}

validateImageDataकरने के लिए सेट false, छवि डेटा के महंगा विश्लेषण के प्रदर्शन के इस प्रकार गंभीर रूप से लोड समय को कम करने से रोकता है GDI +। यह प्रश्न विषय पर अधिक प्रकाश डालता है।


1
मैंने आपके समाधान का उपयोग ICR के ऊपर के समाधान के साथ मिश्रित अंतिम संसाधन के रूप में किया। जेपीईजी के साथ समस्याएं थीं, और इसके साथ हल किया।
जोर्किंड

2
मैंने हाल ही में एक ऐसी परियोजना में यह कोशिश की थी जहाँ मुझे 2000+ चित्रों के आकार (jpg और png ज्यादातर, बहुत मिश्रित आकार) का प्रश्न करना था, और यह वास्तव में पारंपरिक तरीके से उपयोग करने की तुलना में बहुत तेज़ था new Bitmap()
ऐयोनऑफ टाइम

1
सबसे बढ़िया उत्तर। त्वरित, स्वच्छ और प्रभावी।
डायनेमिकल

1
यह फ़ंक्शन विंडोज़ पर एकदम सही है। लेकिन यह लिनक्स पर काम नहीं कर रहा है, यह अभी भी लिनक्स पर पूरी फाइल पढ़ेगा। (.net कोर 2.2)
zhengchun

21

क्या आपने WPF इमेजिंग कक्षाओं का उपयोग करने की कोशिश की है? System.Windows.Media.Imaging.BitmapDecoder, आदि।?

मेरा मानना ​​है कि कुछ प्रयास यह सुनिश्चित करने में थे कि हेडर सूचनाओं को निर्धारित करने के लिए कोडेक्स केवल फ़ाइल का एक सबसेट पढ़ें। यह एक जांच के लायक है।


धन्यवाद। यह उचित लगता है, लेकिन मेरी होस्टिंग में .NET 2.
Jan Zich

1
बहुत बढ़िया जवाब। यदि आप अपने प्रोजेक्ट में PresentationCore का संदर्भ प्राप्त कर सकते हैं, तो यह जाने का तरीका है।

मेरी इकाई परीक्षणों में, ये कक्षाएं GDI से बेहतर प्रदर्शन नहीं करती हैं ... फिर भी JPEGs आयाम पढ़ने के लिए ~ 32K की आवश्यकता होती है।
नरीमन

तो ओपी की छवि आयामों को प्राप्त करने के लिए, आप बिटमैपडेकोडर का उपयोग कैसे करते हैं?
चक सेवेज

1
इस SO प्रश्न को देखें: stackoverflow.com/questions/784734/…
चार्ली

12

मैं कुछ महीने पहले भी कुछ ऐसा ही देख रहा था। मैं एक GIF छवि के प्रकार, संस्करण, ऊंचाई और चौड़ाई को पढ़ना चाहता था, लेकिन ऑनलाइन कुछ भी उपयोगी नहीं पा रहा था।

सौभाग्य से GIF के मामले में, सभी आवश्यक जानकारी पहले 10 बाइट्स में थीं:

Type: Bytes 0-2
Version: Bytes 3-5
Height: Bytes 6-7
Width: Bytes 8-9

पीएनजी थोड़े अधिक जटिल हैं (चौड़ाई और ऊंचाई 4-बाइट्स प्रत्येक हैं):

Width: Bytes 16-19
Height: Bytes 20-23

जैसा कि ऊपर बताया गया है, वाट्सएप छवि और डेटा प्रारूपों पर विस्तृत चश्मे के लिए एक अच्छी साइट है, हालांकि pnglib पर PNG चश्मा बहुत अधिक विस्तृत हैं। हालांकि, मुझे लगता है कि पीएनजी और जीआईएफ प्रारूपों पर विकिपीडिया प्रविष्टि शुरू करने के लिए सबसे अच्छी जगह है।

यहाँ GIF की जाँच के लिए मेरा मूल कोड है, मैंने PNGs के लिए भी एक साथ कुछ थप्पड़ मारे हैं:

using System;
using System.IO;
using System.Text;

public class ImageSizeTest
{
    public static void Main()
    {
        byte[] bytes = new byte[10];

        string gifFile = @"D:\Personal\Images&Pics\iProduct.gif";
        using (FileStream fs = File.OpenRead(gifFile))
        {
            fs.Read(bytes, 0, 10); // type (3 bytes), version (3 bytes), width (2 bytes), height (2 bytes)
        }
        displayGifInfo(bytes);

        string pngFile = @"D:\Personal\Images&Pics\WaveletsGamma.png";
        using (FileStream fs = File.OpenRead(pngFile))
        {
            fs.Seek(16, SeekOrigin.Begin); // jump to the 16th byte where width and height information is stored
            fs.Read(bytes, 0, 8); // width (4 bytes), height (4 bytes)
        }
        displayPngInfo(bytes);
    }

    public static void displayGifInfo(byte[] bytes)
    {
        string type = Encoding.ASCII.GetString(bytes, 0, 3);
        string version = Encoding.ASCII.GetString(bytes, 3, 3);

        int width = bytes[6] | bytes[7] << 8; // byte 6 and 7 contain the width but in network byte order so byte 7 has to be left-shifted 8 places and bit-masked to byte 6
        int height = bytes[8] | bytes[9] << 8; // same for height

        Console.WriteLine("GIF\nType: {0}\nVersion: {1}\nWidth: {2}\nHeight: {3}\n", type, version, width, height);
    }

    public static void displayPngInfo(byte[] bytes)
    {
        int width = 0, height = 0;

        for (int i = 0; i <= 3; i++)
        {
            width = bytes[i] | width << 8;
            height = bytes[i + 4] | height << 8;            
        }

        Console.WriteLine("PNG\nWidth: {0}\nHeight: {1}\n", width, height);  
    }
}

8

अब तक के जवाबों और कुछ अतिरिक्त खोज के आधार पर, ऐसा लगता है कि .NET 2 क्लास लाइब्रेरी में इसके लिए कोई कार्यक्षमता नहीं है। इसलिए मैंने खुद लिखने का फैसला किया। यहाँ इसका बहुत मोटा संस्करण है। फिलहाल, मुझे केवल जेपीजी के लिए इसकी आवश्यकता थी। तो यह अब्बास द्वारा पोस्ट किए गए जवाब को पूरा करता है।

कोई त्रुटि जाँच या अन्य कोई सत्यापन नहीं है, लेकिन मुझे वर्तमान में सीमित कार्य के लिए इसकी आवश्यकता है, और इसे अंततः आसानी से जोड़ा जा सकता है। मैंने इसे कुछ संख्या में छवियों पर परीक्षण किया, और यह आमतौर पर एक छवि से 6K अधिक नहीं पढ़ता है। मुझे लगता है कि यह EXIF ​​डेटा की मात्रा पर निर्भर करता है।

using System;
using System.IO;

namespace Test
{

    class Program
    {

        static bool GetJpegDimension(
            string fileName,
            out int width,
            out int height)
        {

            width = height = 0;
            bool found = false;
            bool eof = false;

            FileStream stream = new FileStream(
                fileName,
                FileMode.Open,
                FileAccess.Read);

            BinaryReader reader = new BinaryReader(stream);

            while (!found || eof)
            {

                // read 0xFF and the type
                reader.ReadByte();
                byte type = reader.ReadByte();

                // get length
                int len = 0;
                switch (type)
                {
                    // start and end of the image
                    case 0xD8: 
                    case 0xD9: 
                        len = 0;
                        break;

                    // restart interval
                    case 0xDD: 
                        len = 2;
                        break;

                    // the next two bytes is the length
                    default: 
                        int lenHi = reader.ReadByte();
                        int lenLo = reader.ReadByte();
                        len = (lenHi << 8 | lenLo) - 2;
                        break;
                }

                // EOF?
                if (type == 0xD9)
                    eof = true;

                // process the data
                if (len > 0)
                {

                    // read the data
                    byte[] data = reader.ReadBytes(len);

                    // this is what we are looking for
                    if (type == 0xC0)
                    {
                        width = data[1] << 8 | data[2];
                        height = data[3] << 8 | data[4];
                        found = true;
                    }

                }

            }

            reader.Close();
            stream.Close();

            return found;

        }

        static void Main(string[] args)
        {
            foreach (string file in Directory.GetFiles(args[0]))
            {
                int w, h;
                GetJpegDimension(file, out w, out h);
                System.Console.WriteLine(file + ": " + w + " x " + h);
            }
        }

    }
}

इसकी कोशिश करने पर चौड़ाई और ऊंचाई उलट जाती है।
जेसन स्टर्ज़

@JasonSturges आपको Exif Orientation टैग को ध्यान में रखना होगा।
एंड्रयू मॉर्टन

3

मैंने पीएनजी फाइल के लिए ऐसा किया था

  var buff = new byte[32];
        using (var d =  File.OpenRead(file))
        {            
            d.Read(buff, 0, 32);
        }
        const int wOff = 16;
        const int hOff = 20;            
        var Widht =BitConverter.ToInt32(new[] {buff[wOff + 3], buff[wOff + 2], buff[wOff + 1], buff[wOff + 0],},0);
        var Height =BitConverter.ToInt32(new[] {buff[hOff + 3], buff[hOff + 2], buff[hOff + 1], buff[hOff + 0],},0);

1

हां, आप ऐसा कर सकते हैं और कोड फ़ाइल प्रारूप पर निर्भर करता है। मैं एक इमेजिंग विक्रेता ( अटलसॉफ्ट ) के लिए काम करता हूं , और हमारा उत्पाद प्रत्येक कोडेक के लिए एक GetImageInfo () प्रदान करता है जो डेटा प्राप्त करने के लिए न्यूनतम आयाम और कुछ अन्य आसान काम करता है।

यदि आप अपना स्वयं का रोल करना चाहते हैं, तो मैं सुझाव देता हूं कि wotsit.org के साथ शुरुआत करें , जिसमें बहुत अधिक सभी छवि प्रारूपों के लिए विस्तृत चश्मा है और आप देखेंगे कि फ़ाइल की पहचान कैसे करें और इसमें भी जानकारी कहां मिल सकती है।

यदि आप सी के साथ काम करने में सहज हैं, तो इस जानकारी को भी प्राप्त करने के लिए निशुल्क जेपीग्लिब का उपयोग किया जा सकता है। मैं शर्त लगाता हूं कि आप .NET लाइब्रेरी के साथ ऐसा कर सकते हैं, लेकिन मुझे नहीं पता कि कैसे।


क्या यह मान लेना सुरक्षित है कि उपयोग new AtalaImage(filepath).Widthकुछ इसी तरह करता है?
drzaus


1
पहला (AtalaImage) पूरी छवि को पढ़ता है - दूसरा (GetImageInfo) छवि जानकारी ऑब्जेक्ट के तत्वों को प्राप्त करने के लिए न्यूनतम मेटाडेटा पढ़ता है।
लो फ्रेंको

0

प्रगतिशील IPegs और WebP का समर्थन करने के लिए ICR का उत्तर अपडेट किया गया :)

internal static class ImageHelper
{
    const string errorMessage = "Could not recognise image format.";

    private static Dictionary<byte[], Func<BinaryReader, Size>> imageFormatDecoders = new Dictionary<byte[], Func<BinaryReader, Size>>()
    {
        { new byte[] { 0x42, 0x4D }, DecodeBitmap },
        { new byte[] { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 }, DecodeGif },
        { new byte[] { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }, DecodeGif },
        { new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, DecodePng },
        { new byte[] { 0xff, 0xd8 }, DecodeJfif },
        { new byte[] { 0x52, 0x49, 0x46, 0x46 }, DecodeWebP },
    };

    /// <summary>        
    /// Gets the dimensions of an image.        
    /// </summary>        
    /// <param name="path">The path of the image to get the dimensions of.</param>        
    /// <returns>The dimensions of the specified image.</returns>        
    /// <exception cref="ArgumentException">The image was of an unrecognised format.</exception>            
    public static Size GetDimensions(BinaryReader binaryReader)
    {
        int maxMagicBytesLength = imageFormatDecoders.Keys.OrderByDescending(x => x.Length).First().Length;
        byte[] magicBytes = new byte[maxMagicBytesLength];
        for(int i = 0; i < maxMagicBytesLength; i += 1)
        {
            magicBytes[i] = binaryReader.ReadByte();
            foreach(var kvPair in imageFormatDecoders)
            {
                if(StartsWith(magicBytes, kvPair.Key))
                {
                    return kvPair.Value(binaryReader);
                }
            }
        }

        throw new ArgumentException(errorMessage, "binaryReader");
    }

    private static bool StartsWith(byte[] thisBytes, byte[] thatBytes)
    {
        for(int i = 0; i < thatBytes.Length; i += 1)
        {
            if(thisBytes[i] != thatBytes[i])
            {
                return false;
            }
        }

        return true;
    }

    private static short ReadLittleEndianInt16(BinaryReader binaryReader)
    {
        byte[] bytes = new byte[sizeof(short)];

        for(int i = 0; i < sizeof(short); i += 1)
        {
            bytes[sizeof(short) - 1 - i] = binaryReader.ReadByte();
        }
        return BitConverter.ToInt16(bytes, 0);
    }

    private static int ReadLittleEndianInt32(BinaryReader binaryReader)
    {
        byte[] bytes = new byte[sizeof(int)];
        for(int i = 0; i < sizeof(int); i += 1)
        {
            bytes[sizeof(int) - 1 - i] = binaryReader.ReadByte();
        }
        return BitConverter.ToInt32(bytes, 0);
    }

    private static Size DecodeBitmap(BinaryReader binaryReader)
    {
        binaryReader.ReadBytes(16);
        int width = binaryReader.ReadInt32();
        int height = binaryReader.ReadInt32();
        return new Size(width, height);
    }

    private static Size DecodeGif(BinaryReader binaryReader)
    {
        int width = binaryReader.ReadInt16();
        int height = binaryReader.ReadInt16();
        return new Size(width, height);
    }

    private static Size DecodePng(BinaryReader binaryReader)
    {
        binaryReader.ReadBytes(8);
        int width = ReadLittleEndianInt32(binaryReader);
        int height = ReadLittleEndianInt32(binaryReader);
        return new Size(width, height);
    }

    private static Size DecodeJfif(BinaryReader binaryReader)
    {
        while(binaryReader.ReadByte() == 0xff)
        {
            byte marker = binaryReader.ReadByte();
            short chunkLength = ReadLittleEndianInt16(binaryReader);
            if(marker == 0xc0 || marker == 0xc2) // c2: progressive
            {
                binaryReader.ReadByte();
                int height = ReadLittleEndianInt16(binaryReader);
                int width = ReadLittleEndianInt16(binaryReader);
                return new Size(width, height);
            }

            if(chunkLength < 0)
            {
                ushort uchunkLength = (ushort)chunkLength;
                binaryReader.ReadBytes(uchunkLength - 2);
            }
            else
            {
                binaryReader.ReadBytes(chunkLength - 2);
            }
        }

        throw new ArgumentException(errorMessage);
    }

    private static Size DecodeWebP(BinaryReader binaryReader)
    {
        binaryReader.ReadUInt32(); // Size
        binaryReader.ReadBytes(15); // WEBP, VP8 + more
        binaryReader.ReadBytes(3); // SYNC

        var width = binaryReader.ReadUInt16() & 0b00_11111111111111; // 14 bits width
        var height = binaryReader.ReadUInt16() & 0b00_11111111111111; // 14 bits height

        return new Size(width, height);
    }

}

-1

यह फ़ाइल स्वरूप पर निर्भर करने वाला है। आमतौर पर वे इसे फाइल के शुरुआती बाइट्स में बताएंगे। और, आमतौर पर, एक अच्छी छवि-पठन कार्यान्वयन को ध्यान में रखा जाएगा। मैं आपको .NET के लिए एक की ओर इशारा नहीं कर सकता।

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.