转换为.Net 2.0 [英] Convert to .Net 2.0

查看:70
本文介绍了转换为.Net 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


此代码适用于.Net 3.5.抱歉,有人可以将此代码转换为.Net 2.0吗?

Hi
This code is for .Net 3.5. I am sorry, can anybody convert this code to .Net 2.0?

public 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 },
 };
     /// <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(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 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 (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);
     }
 }

推荐答案

int maxMagicBytesLength = imageFormatDecoders.Keys.OrderByDescending(x => x.Length).First().Length;


您必须使用delegate的.net 2.0方式,而不是.net 3.5 lambda表达式.
只需在msdn上查找lambda表达式即可.如果您学习了这个简单的概念,就不会失去任何东西.然后,您可以轻松地将其转换为.net 2.0标准delegate.


You have to use delegates .net 2.0 way, not .net 3.5 lambda expressions.
Just look for lambda expressions on msdn. You will not lose anything if you learn this simple concept. You will then easily be able to convert it to .net 2.0 standard delegates.


如果您使用VS 2008或2010,则只需将目标框架从3.5更改为2.0.然后,当您尝试编译时,会出现错误,告诉您确切需要更改的内容.

最有可能的是,您不需要进行太多更改-如果有的话...

您也可以尝试以下操作:

SolutionConverter [
If you use VS 2008 or 2010 you can simply change the target framework from 3.5 to 2.0. Then when you try to compile, you will get errors telling you exactly what you need to change.

Most likely, you don''t need to make that many changes - if any at all...

You could also try this:

SolutionConverter[^]


先进行备份,然后使用Web Project Conversion Wizard迁移您的应用程序.这里有很多有用的信息:

从ASP.NET 1.x升级
http://msdn2.microsoft.com/en-us/asp.net/aa336650.aspx
make a back up first and then use the Web Project Conversion Wizard to migrate your application. Theres lots of useful information here:

Upgrade from ASP.NET 1.x
http://msdn2.microsoft.com/en-us/asp.net/aa336650.aspx


这篇关于转换为.Net 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆