【发布时间】: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 - 1到height - 1并重置h after each loop toheight - 1`。 - 显然,这是迄今为止翻转位图的 worst 方法。查看Image.RotateFlip!
标签: c# image image-processing graphics