【问题标题】:Manual Image Flip Code C#手动图像翻转代码 C#
【发布时间】:2016-07-17 23:05:18
【问题描述】:

我想手动翻转图像,我有两个图片框,我将原始图像加载到 1 个框中并在其他框中进行操作,我正在尝试的算法是从原始像素读取原始图像并将该像素放入空白图像的右上角等等,我的代码不起作用,无论我做什么,它总是抛出异常>_

**************异常文本************** System.ArgumentOutOfRangeException:参数必须为正且

在这里

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ImageFlip
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Bitmap temp;
            OpenFileDialog Dialog = new OpenFileDialog();
            Dialog.Title = "Open an image file";
            if (Dialog.ShowDialog() == DialogResult.OK)
                pictureBox1.Image = System.Drawing.Image.FromFile(Dialog.FileName);
            temp = (Bitmap)pictureBox1.Image;

        }
        private Bitmap flip (Bitmap image)
        {
            Bitmap tp = new Bitmap(image.Width, image.Height);
            int x, y;
            int w=image.Width, h=image.Height;
            int w = 100, h = 100;
            //MessageBox.Show(image.Width.ToString());
            //MessageBox.Show(image.Height.ToString());
            Color pix;
            for (x = 0; x <= image.Width; x++)
            {
                for (y = 0; y <= image.Height; y++)
                {
                    pix = image.GetPixel(x, y);
                    tp.SetPixel(w, h, pix);
                    h--;
                }
                w--;
            }                    
            return tp;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            pictureBox2.Image = flip((Bitmap)pictureBox1.Image);
        }
    }
}

【问题讨论】:

  • 有什么异常?请将其应用于您的问题
  • 有关调用即时 (JIT) 调试而不是此对话框的详细信息,请参见此消息的末尾。 ************** 异常文本 ************** System.ArgumentOutOfRangeException:参数必须为正且
  • w 和 h 不应该是 99 而不是 100? (你的 for 循环从 0 到 99)
  • 我试过了,还是一样的异常:'(
  • 如发布的那样,您应该从 width - 1height - 1 并重置 h after each loop to height - 1`。 - 显然,这是迄今为止翻转位图的 worst 方法。查看Image.RotateFlip

标签: c# image image-processing graphics


【解决方案1】:

您发布的代码旋转图像(如果是方形,否则会失败)

开始复制列时需要重置h

var w=image.Width - 1;
for (x = 0; x < image.Width; x++)
{
    var h=image.Height - 1;
    for (y = 0; y < image.Height; y++)
    {
        pix = image.GetPixel(x, y);
        tp.SetPixel(w, h, pix);
        h--;
    }
    w--;
 }  

要在垂直轴上镜像/翻转图像,只需省略 h 并使用 y

var w=image.Width - 1;
for (x = 0; x < image.Width; x++)
{
    for (y = 0; y < image.Height; y++)
    {
        pix = image.GetPixel(x, y);
        tp.SetPixel(w, y, pix);
    }
    w--;
 }  

要在水平轴上镜像/翻转图像,请省略 w 并使用 x

for (x = 0; x < image.Width; x++)
{
    var h=image.Height - 1;
    for (y = 0; y < image.Height; y++)
    {
        pix = image.GetPixel(x, y);
        tp.SetPixel(x, h, pix);
        h--;
    }
 } 

您甚至可以完全删除 wh

for (x = 0; x < image.Width; x++)
{
    for (y = 0; y < image.Height; y++)
    {
        pix = image.GetPixel(x, y);
        tp.SetPixel(image.Width - 1 - x, y, pix);
        // or
        // tp.SetPixel(x, image.Height - 1 - y, pix);
    }
 }  

【讨论】:

  • 干杯,这对我有用,非常感谢。第一次完美运行,我知道它正在将图像旋转 180* 度,但我应该镜像图像,因为更改我的代码需要什么?
  • 在 SetPixel 中将 w 替换为 x 或将 h 替换为 y,这取决于它应该镜像的方式
  • @AhsanAlii - 我添加了镜像和简化版本。
【解决方案2】:

您正在从 0 遍历到

您应该考虑改为从 0 到

for (x = 0; x < image.Width; x++)
            {
                for (y = 0; y < image.Height; y++)

【讨论】:

  • 仍然抛出异常:'(
  • 只要检查你的范围,还有 w 和 h 参数。您遇到的是超出有效像素范围。尝试设置 x = 1, y = 1 看看是否对您有帮助。宽度 = 100 表示有效像素存在于 0 到 99 之间。每次循环后 h 也需要回到 0
  • 所有参数和值对我来说都是有效的,但得到异常 int x, y; int w=image.Width, h=image.Height;彩色像素; for (x = 0; x
猜你喜欢
  • 2015-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多