System.Drawing错误:参数无效

时间:2017-05-12 08:37:14

标签: c# image parameters stream system.drawing

当我尝试保存图片时,我在使用c#处理图片时遇到了问题。错误来自system.drawing,表示参数无效,这是我的完整代码,有什么想法吗?! 感谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;
using System.Collections;

namespace ipp
{
    class Program
    {
        public static byte[] ImageToArray(Image image,
  System.Drawing.Imaging.ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                // Convert Image to byte[]
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();
                return imageBytes;
            }
        }
        public static Image ArrayToImage(byte[] imageBytes)
        {
            using (var ms = new MemoryStream(imageBytes))
            {
                return Image.FromStream(ms);
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Welcome, The Operation Has Begun.");
            var img = new System.Drawing.Bitmap(@"E:\gray.bmp");
            byte[] image1 = ImageToArray(img, System.Drawing.Imaging.ImageFormat.Bmp);
            BitArray bits = new BitArray(image1);
            byte[] subar0 = new byte[image1.Length];
            byte[] subar1 = new byte[image1.Length];
            byte[] subar2 = new byte[image1.Length];
            byte[] subar3 = new byte[image1.Length];
            byte[] subar4 = new byte[image1.Length];
            byte[] subar5 = new byte[image1.Length];
            byte[] subar6 = new byte[image1.Length];
            byte[] subar7 = new byte[image1.Length];
            byte[] subarf = new byte[image1.Length];
            for (int i = 0; i < bits.Length; i += 8)
            {
                subar0[i / 8] = Convert.ToByte(bits[i]);
            }
            for (int i = 1; i < bits.Length; i += 8)
            {
                subar1[i / 8] = Convert.ToByte(bits[i]);
            }
            for (int i = 2; i < bits.Length; i += 8)
            {
                subar2[i / 8] = Convert.ToByte(bits[i]);
            }
            for (int i = 3; i < bits.Length; i += 8)
            {
                subar3[i / 8] = Convert.ToByte(bits[i]);
            }
            for (int i = 4; i <= bits.Length; i += 8)
            {
                subar4[i / 8] = Convert.ToByte(bits[i]);
            }
            for (int i = 5; i < bits.Length; i += 8)
            {
                subar5[i / 8] = Convert.ToByte(bits[i]);
            }
            for (int i = 6; i < bits.Length; i += 8)
            {
                subar6[i / 8] = Convert.ToByte(bits[i]);
            }
            for (int i = 7; i < bits.Length; i += 8)
            {
                subar7[i / 8] = Convert.ToByte(bits[i]);
            }
            for (int i = 0; i < image1.Length; i++)
            {
                subarf[i] = Convert.ToByte(128 * subar0[i] + 64 * subar1[i]);
            }

            Image fout = ArrayToImage(subarf);
            fout.Save("E:\\merge.bmp", System.Drawing.Imaging.ImageFormat.Bmp);



        }
    }
}

0 个答案:

没有答案