【C#】图片处理汇总

发布时间 2023-09-15 17:28:23作者: 不溯流光

一、图片与字节数组互转

        private void Test()
        {
            string picPath = @"C:\Users\Public\Pictures\Sample Pictures\Cat.jpg";


            byte[] photo = ImageToByte(picPath);
            pictureBox1.Image = ByteToImage(photo);

            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
        }

        private byte[] ImageToByte(string path)
        {
            byte[] photo;
            Image img = new Bitmap(path);
            MemoryStream stream = new MemoryStream();
            img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
            photo = stream.ToArray();
            stream.Close();
            return photo;
        }

        private Image ByteToImage(byte[] photo)
        {
            MemoryStream ms = new MemoryStream(photo, true);
            Image img1 = Image.FromStream(ms);
            return img1;
        }