简体   繁体   中英

Digital Image Processing Saving an Image

I am trying to save an image I have processed in my program. eg added pseudocolor to the original image

I have tried a few methods, each time i get an error.

A generic error occurred in GDI+.

Can anyone tell me how to properly implement a save method?

Here is the latest attempt:

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

        }

            }

        }
if (save.ShowDialog() == DialogResult.OK)
{
    proc_image.Save(save.FileName);
}

Can't save some thing as a directory, and even if you could you wouldn't want to use the initial directory..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM