要将BMP图像翻转,我的想法是把图像上下两部分的象素点颜色值上下对调,下面是我的程序但有错误,请帮忙改改,谢谢using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace IMAGE
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItemOpen;
private System.Windows.Forms.MenuItem menuItemSave;
private System.Windows.Forms.MenuItem menuItemExit;
private System.Drawing.Bitmap bitmap;
private System.Windows.Forms.MenuItem menuItemEdit;
private System.Windows.Forms.MenuItem menuItemInvert;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
            bitmap=new Bitmap(1,1);
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItemOpen = new System.Windows.Forms.MenuItem();
this.menuItemSave = new System.Windows.Forms.MenuItem();
this.menuItemExit = new System.Windows.Forms.MenuItem();
this.menuItemEdit = new System.Windows.Forms.MenuItem();
this.menuItemInvert = new System.Windows.Forms.MenuItem();
// 
// mainMenu1
// 
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  this.menuItem1,
  this.menuItemEdit});
// 
// menuItem1
// 
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  this.menuItemOpen,
  this.menuItemSave,
  this.menuItemExit});
this.menuItem1.Text = "文件";
// 
// menuItemOpen
// 
this.menuItemOpen.Index = 0;
this.menuItemOpen.Text = "打开";
this.menuItemOpen.Click += new System.EventHandler(this.menuItemOpen_Click);
// 
// menuItemSave
// 
this.menuItemSave.Index = 1;
this.menuItemSave.Text = "保存";
this.menuItemSave.Click += new System.EventHandler(this.menuItemSave_Click);
// 
// menuItemExit
// 
this.menuItemExit.Index = 2;
this.menuItemExit.Text = "退出";
this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click);
// 
// menuItemEdit
// 
this.menuItemEdit.Enabled = false;
this.menuItemEdit.Index = 1;
this.menuItemEdit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
 this.menuItemInvert});
this.menuItemEdit.Text = "编辑";
// 
// menuItemInvert
// 
this.menuItemInvert.Index = 0;
this.menuItemInvert.Text = "翻转";
this.menuItemInvert.Click += new System.EventHandler(this.menuItemInvert_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(616, 385);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
}

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(bitmap, new Rectangle(this.AutoScrollPosition.X, this.AutoScrollPosition.Y,
(int)(bitmap.Width), (int)(bitmap.Height)));
} private void menuItemExit_Click(object sender, System.EventArgs e)
{
Application.Exit();
} private void menuItemOpen_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog(); 
openFileDialog.Filter = "Bitmap文件(*.bmp)|*.bmp"; 
openFileDialog.RestoreDirectory = true ; 
if(DialogResult.OK == openFileDialog.ShowDialog()) 

bitmap =new Bitmap(openFileDialog.FileName); 
menuItemEdit.Enabled=true;
this.AutoScroll = true;
this.AutoScrollMinSize=new Size ((int)(bitmap.Width),(int)bitmap.Height);
this.Invalidate();
}
} private void menuItemSave_Click(object sender, System.EventArgs e)
{
SaveFileDialog saveFileDialog=new SaveFileDialog();
saveFileDialog.Filter="Bitmap文件(*.bmp)|*.bmp";
saveFileDialog.RestoreDirectory =true;
if(saveFileDialog.ShowDialog()==DialogResult.OK)
bitmap.Save(saveFileDialog.FileName);
} private void menuItemInvert_Click(object sender, System.EventArgs e)
{
int x,y,mid;
Color color;
x=bitmap.Width;
y=bitmap.Height;
mid=y/2;
for(int i=0,j=y;i<mid;i++,y--)
for(int k=0;k<x;k++)
{
      color=bitmap.GetPixel(i,k);
bitmap.SetPixel(i,k,bitmap.GetPixel(j,k));
bitmap.SetPixel(j,k,color);   
}

this.Invalidate();
}
}
}
提示出错:An unhandled exception of type 'System.Exception' occurred in system.drawing.dllAdditional information: SetPixel is not supported for images with indexed pixel formats.

解决方案 »

  1.   

    int width = image.Width;
    int height = image.Height;
    #region
    if(type==0)
    {
    for(int i=0;i<width/2;i++)
    {
    for(int j=0;j<height;j++)
    {
    if(i!=width-1-i)
    {
    Color color = image.GetPixel(i,j);
    image.SetPixel(i,j,image.GetPixel(width-1-i,j));
    image.SetPixel(width-1-i,j,color);
    }
    }
    }
    }
    else if(type==1)
    {
    for(int i=0;i<width;i++)
    {
    for(int j=0;j<height/2;j++)
    {
    if(j!=height-1-j)
    {
    Color color = image.GetPixel(i,j);
    image.SetPixel(i,j,image.GetPixel(i,height-1-j));
    image.SetPixel(i,height-1-j,color);
    }
    }
    }
    }
    else if(type==2)
    {
    for(int i=0;i<width;i++)
    {
    for(int j=0;j<height/2;j++)
    {
    Color color = image.GetPixel(i,j);
    image.SetPixel(i,j,image.GetPixel(width-1-i,height-1-j));
    image.SetPixel(width-1-i,height-1-j,color);
    }
    }任意翻转
      

  2.   

    提示出错:An unhandled exception of type 'System.Exception' occurred in system.drawing.dllAdditional information: SetPixel is not supported for images with indexed pixel formats.还是提示出错
      

  3.   

    不用这么复杂吧,用矩阵变换就可以啦!
    Dim bmp As New Bitmap(Me.PictureBox1.Image.Width, Me.PictureBox1.Image.Height)
    Dim g As Drawing.Graphics = Drawing.Graphics.FromImage(bmp)
    Dim m As New Drawing2D.Matrix
    Dim m1 As New Drawing2D.Matrix
    Dim m2 As New Drawing2D.Matrix(1, 0, 0, -1, 0, bmp.Height) '*** 反射并平移
    m1.Multiply(m2)
    g.Transform = m1
    g.DrawImage(Me.PictureBox1.Image, 0, 0)
    Me.PictureBox2.Image = bmp
      

  4.   

    ReinhardCao(ReinhardCao)的方法不错,长见识了