我想手动翻转一个图像,我有两个图片框,我在一个框中加载原始图像,并在另一个框进行操作,我尝试的算法是从原始像素读取原始图像,然后将该像素放在空白图像的右上角,以此类推,我的代码不起作用,无论我做什么,它总是抛出一个异常>_<有关调用的详细信息,请参阅此消息的末尾实时(JIT)调试,而不是此对话框。
**************异常文本**************System.ArgumentOutOfRangeException:参数必须为正并且<身高参数名称:y在System.Drawing.Bitmap.GetPixel(Int32 x,Int32 y)在c:\users\ahsan\documents\visualstudio中的ImageFlip.Form1.flip(位图图像)2015\Projects\ImageFlip\Form1.cs:line 43在c:\users\ahsan\documents\visual studio中的ImageFlip.Form1.button2_Click(Object sender,EventArgs e)2015\Projects\ImageFlip\Form1.cs:line 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,鼠标按钮按钮,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(消息&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);
}
}
}
您发布的代码会旋转图像(如果图像是方形的,否则会失败)
开始复制列时,需要重置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--;
}
}
您甚至可以完全删除w
和h
:
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);
}
}
您正在从0遍历到<=图像.宽度(和高度)
您应该考虑从0到<image.width(和height)。错误是因为没有像素100。
for (x = 0; x < image.Width; x++)
{
for (y = 0; y < image.Height; y++)