手动图像翻转代码C#

时间:2016-03-30 05:32:28

标签: c# image image-processing graphics

我想手动翻转图像,我有两个图片框,我将原始图像加载到一个框中并在另一个框中操作,我正在尝试的算法是从原始像素读取原始图像并将该像素放入空白图像的右上角等等,我的代码无法正常工作,无论我做什么,它总是抛出异常> _< 有关调用的详细信息,请参阅此消息的结尾 实时(JIT)调试而不是此对话框。

  

**************例外文字**************       System.ArgumentOutOfRangeException:参数必须为正且<高度。       参数名称:y          在System.Drawing.Bitmap.GetPixel(Int32 x,Int32 y)          在C:\ users \ ahsan \ documents \ visual studio中的ImageFlip.Form1.flip(位图图像​​)   2015 \ Projects \ ImageFlip \ Form1.cs:第43行          at ImageFlip.Form1.button2_Click(Object sender,EventArgs e)在c:\ users \ ahsan \ documents \ visual studio中   2015 \ Projects \ ImageFlip \ Form1.cs:第54行          在System.Windows.Forms.Control.OnClick(EventArgs e)          在System.Windows.Forms.Button.OnClick(EventArgs e)          在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)          在System.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons按钮,Int32点击)          在System.Windows.Forms.Control.WndProc(消息& m)          在System.Windows.Forms.ButtonBase.WndProc(消息& m)          在System.Windows.Forms.Button.WndProc(消息& m)          在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)          在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)          在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

这是它

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);
        }
    }
}

2 个答案:

答案 0 :(得分: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);
    }
 }  

答案 1 :(得分:0)

您从0到&lt; = image.width(和height)

你应该考虑从0到&lt;而是image.width(和height)。错误是因为没有像素100.

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