c# 分成两幅图像/流转位图

问题描述 投票:0回答:1

我使用 Visual Studio 2022 C# 语言。 (新手...)。我想将一张图像分成两部分(分成两半)。分割图像中的第一张图像输入到第一个 picutrebox,然后另一个第二图像也输入到第二个 picutrebox。

所以我首先尝试输入一张图像到图片框(尚未分割)。 - 完成了吗

FileStream myFileStream = new FileStream(imagepath, FileMode.Open, FileAccess.Read);
MemoryStream myStream = new MemoryStream();
byte[] photo = new byte[myFileStream.Length];

myFileStream.Read(photo, 0, photo.Length);
myStream.Write(photo, 0, photo.Length);

drawingImage = new Bitmap(myStream); -> //no error

然后我尝试以一种非常简单的方式分割图像,我认为只需除以 2。我认为可以将除以大小的值放入字节变量中。我做了一些修改。就像这里:

byte[] photo = new byte[myFileStream.Length / 2]; -> //just divide by 2

myFileStream.Read(photo, 0, photo.Length); //-> I thought I could only read the length.
myStream.Write(photo, 0, photo.Length);

drawingImage = new Bitmap(myStream); -> //error -> System.ArgumentException:"Invalid parameter."

最终目标是分割大容量的图像。

如何解决?

c# image bitmap large-files
1个回答
0
投票

我使用了一个在 Windows 窗体应用程序上使用 2 个图片框的示例。图像被加载然后分成两部分,然后每一半显示在每个图片框

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace ImageSplitter
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // Replace this with the path to your image
            string imagePath = "path_to_your_image.jpg";

            // Load and split the image
            SplitImage(imagePath);
        }

        private void SplitImage(string imagePath)
        {
            try
            {
                using (FileStream myFileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
                {
                    Bitmap originalImage = new Bitmap(myFileStream);

                    // Calculate the width of each half
                    int halfWidth = originalImage.Width / 2;

                    // Create two new Bitmaps for each half
                    Bitmap firstHalf = new Bitmap(halfWidth, originalImage.Height);
                    Bitmap secondHalf = new Bitmap(halfWidth, originalImage.Height);

                    using (Graphics g = Graphics.FromImage(firstHalf))
                    {
                        // Draw the first half of the original image
                        g.DrawImage(originalImage, new Rectangle(0, 0, halfWidth, originalImage.Height), new Rectangle(0, 0, halfWidth, originalImage.Height), GraphicsUnit.Pixel);
                    }

                    using (Graphics g = Graphics.FromImage(secondHalf))
                    {
                        // Draw the second half of the original image
                        g.DrawImage(originalImage, new Rectangle(0, 0, halfWidth, originalImage.Height), new Rectangle(halfWidth, 0, halfWidth, originalImage.Height), GraphicsUnit.Pixel);
                    }

                    // Display each half in the PictureBoxes
                    pictureBox1.Image = firstHalf;
                    pictureBox2.Image = secondHalf;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred: " + ex.Message);
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.