我正在尝试保存我在程序中处理过的图像。例如在原始图像中添加伪彩色
我尝试了几种方法,每次都出现错误。
GDI+ 中出现一般错误。
谁能告诉我如何正确实现保存方法?
这是最新的尝试:
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace IMGPROC
{
public partial class Form1 : Form
{
public Bitmap original_image, proc_image;
public Form1()
{
InitializeComponent();
original_image = null;
proc_image = null;
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void Form1_Paint(object sender, PaintEventArgs e)
{
if (original_image != null)
{
Graphics g = e.Graphics;
Rectangle r = new Rectangle(10, 50, original_image.Width, original_image.Height);
g.DrawImage(original_image, r);
}
}
// OPEN IMAGE FILE
/******************************************************************************************/
private void openToolStripMenuItem_Click_1(object sender, EventArgs e)
{
// show the openFile dialog box
Graphics g = this.CreateGraphics();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
original_image = new Bitmap(openFileDialog1.FileName);
}
Rectangle r = new Rectangle(10, 50, original_image.Width, original_image.Height);
g.DrawImage(original_image, r);
}
// SAVE IMAGE FILE
/******************************************************************************************/
private void saveAsToolStripMenuItem_Click(object sender, System.EventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = "Bitmap Image (.bmp)|*.bmp|Gif Image (.gif)|*.gif |JPEG Image (.jpeg)|*.jpeg |Png Image (.png)|*.png |Tiff Image (.tiff)|*.tiff |Wmf Image (.wmf)|*.wmf |All files (*.*)|*.*";
save.FilterIndex = 4;
save.InitialDirectory = "C:\\";
save.RestoreDirectory = true;
if (save.ShowDialog() == DialogResult.OK)
{
proc_image.Save(save.InitialDirectory);
}
}
// EXIT APPLICATION
/************************************************************************************/
private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
{
Dispose();
Application.Exit();
}
private void btnRed_Click(object sender, System.EventArgs e)
{
Graphics g = this.CreateGraphics();
int width = original_image.Width;
int height = original_image.Height;
Color pixel;
Rectangle r = new Rectangle(535, 50, original_image.Width, original_image.Height);
Rectangle r2 = new Rectangle(0, 0, original_image.Width, original_image.Height);
Rectangle r3 = new Rectangle(10, 50, original_image.Width, original_image.Height);
g.DrawImage(original_image, r3);
Bitmap proc_image = new Bitmap(width, height, PixelFormat.Format24bppRgb);
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
pixel = original_image.GetPixel(x, y);
proc_image.SetPixel(x, y, Color.FromArgb(pixel.R, 0, 0));
}
} g.DrawImage(proc_image, r);
}
private void btnGreen_Click(object sender, System.EventArgs e)
{
Graphics g = this.CreateGraphics();
int width = original_image.Width;
int height = original_image.Height;
Color pixel;
Rectangle r = new Rectangle(535, 50, original_image.Width, original_image.Height);
Rectangle r2 = new Rectangle(0, 0, original_image.Width, original_image.Height);
Rectangle r3 = new Rectangle(10, 50, original_image.Width, original_image.Height);
g.DrawImage(original_image, r3);
Bitmap bitmap_colour = new Bitmap(width, height, PixelFormat.Format24bppRgb);
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
pixel = original_image.GetPixel(x, y);
bitmap_colour.SetPixel(x, y, Color.FromArgb(0, pixel.G, 0));
}
} g.DrawImage(bitmap_colour, r);
}
private void btnBlue_Click(object sender, System.EventArgs e)
{
Graphics g = this.CreateGraphics();
int width = original_image.Width;
int height = original_image.Height;
Color pixel;
Rectangle r = new Rectangle(535, 50, original_image.Width, original_image.Height);
Rectangle r2 = new Rectangle(0, 0, original_image.Width, original_image.Height);
Rectangle r3 = new Rectangle(10, 50, original_image.Width, original_image.Height);
g.DrawImage(original_image, r3);
Bitmap bitmap_colour = new Bitmap(width, height, PixelFormat.Format24bppRgb);
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
pixel = original_image.GetPixel(x, y);
bitmap_colour.SetPixel(x, y, Color.FromArgb(0, 0, pixel.B));
}
} g.DrawImage(bitmap_colour, r);
}
private void pseudocolorToolStripMenuItem_Click(object sender, System.EventArgs e)
{
checkImageOpen();
btnRed.Visible = true;
btnGreen.Visible = true;
btnBlue.Visible = true;
Graphics g = this.CreateGraphics();
int width = original_image.Width;
int height = original_image.Height;
Rectangle r = new Rectangle(535, 50, original_image.Width, original_image.Height);
Rectangle r2 = new Rectangle(0, 0, original_image.Width, original_image.Height);
Rectangle r3 = new Rectangle(10, 50, original_image.Width, original_image.Height);
g.DrawImage(original_image, r3);
}
}
}