Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Hi I'm trying to create an negative image.

This is the code I use to do it. I got it from the Bob Powell FAQ.

private void button2_Click(object sender, EventArgs e)
       {
           if(this.pictureBox1.Visible == true)
           {
               Image img = this.pictureBox1.Image;

               Bitmap copy = new Bitmap(img.Width,img.Height);

               ImageAttributes ia = new ImageAttributes();


               ColorMatrix cm = new ColorMatrix();
               cm.Matrix00 = cm.Matrix11 = cm.Matrix22 = 0.99f;
               cm.Matrix33 = cm.Matrix44 = 1;
               cm.Matrix40 = cm.Matrix41 = cm.Matrix42 = .04f;

                               
               ia.SetColorMatrix(cm);

               
               Graphics g = Graphics.FromImage(copy);
          
               g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);



               g.Dispose();

               img.Dispose();
           }

The problem I have is, when I'm click the button I get an out of memory exception at the g.DrawImage line.

 

Thanks for the help

Edited by PlausiblyDamp
Posted
What specs are your PC, also what sized Image are you using. I ran the code which you posted without any errors. On a side note, it simply made the image lighter not a negative.
Anybody looking for a graduate programmer (Midlands, England)?
Posted
What specs are your PC' date=' also what sized Image are you using. I ran the code which you posted without any errors. On a side note, it simply made the image lighter not a negative.[/quote']

 

I ran a Pentium 3Ghz with 1GB Ram and a Geforce 6600gt. So I think that isn't the problem.

The images are all not bigger than 800Kb normally pics under 100Kb the resolution is also small.

 

It's always the same other people try the code and it is allright!

:(

Posted
Try adding the code to a new project just to check and see if that solves your problem. There doesn't appear to be anything inherantly wrong with that code.
Anybody looking for a graduate programmer (Midlands, England)?
Posted
Seems unlikely your missing a dll due to the error. Are you using .Net 2.0 or 1.0/1.1? I'm using 1.1 and had no trouble at all with the code you have posted.
Anybody looking for a graduate programmer (Midlands, England)?
Posted
Seems unlikely your missing a dll due to the error. Are you using .Net 2.0 or 1.0/1.1? I'm using 1.1 and had no trouble at all with the code you have posted.

 

I'm using 2.0...maybe that's the prob I will try it :-\

Posted

Hmm, works for me with 1 alteration:

 

picturebox1.Image = copy;

Before disposing of img.

 

Otherwise you are disposing of the pictureboxes active image.

Posted

I think I'm going crazy here the hole code plz try it that would be nice

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Net;
using System.Drawing.Imaging;

namespace ActiveXNet
{
   public partial class ucMain : UserControl
   {

       public ucMain()
       {
           InitializeComponent();
       }


       private void ucMain_Load(object sender, EventArgs e)
       {

       }

       private void button1_Click(object sender, EventArgs e)
       {

           openFileDialog.Multiselect = true;
           openFileDialog.Filter = "Alle | *.AVI;*.MPG;*.MPEG;*.WMV;*.MOV;*.QT;*.JPG;*.BMP;*.swf |Videos | *.AVI;*.MPG;*.MPEG;*.WMV;*.MOV;*.QT |Bilder | *.JPG;*.BMP |Flash | *.swf";
           openFileDialog.ShowDialog(this);

           if (openFileDialog.FileNames.Length > 0)
           {
               for (int i = 0; i != openFileDialog.FileNames.Length; i++)
               {

                   string filename = System.IO.Path.GetFileName(openFileDialog.FileNames[i]);

                   listBox1.Items.Add(filename);
                   string filepath = System.IO.Path.GetFullPath(openFileDialog.FileNames[i]);
                   string[] path = { filepath };
                   foreach (string paths in path)
                   {
                       listBox2.Items.Add(paths);
                   }

                   //MessageBox.Show("File Location: " + openFileDialog.FileNames[i]);
               }

           }
       }

       private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
       {
           if (listBox1.SelectedIndex != -1)
           {
               this.pictureBox1.Visible = false;
               this.axWindowsMediaPlayer1.Visible = false;
               this.axWindowsMediaPlayer1.Ctlcontrols.stop();

               listBox2.SetSelected(listBox1.SelectedIndex, true);

               string extension = System.IO.Path.GetExtension(listBox2.Text);

               if (extension.ToLower() == ".jpg")
               {
                   this.pictureBox1.ImageLocation = listBox2.Text;
                   this.pictureBox1.Visible = true;
               }
               else if (extension.ToLower() == ".bmp")
               {
                   this.pictureBox1.ImageLocation = listBox2.Text;
                   this.pictureBox1.Visible = true;
               }
               else if (extension.ToLower() == ".swf")
               {
                   this.pictureBox1.ImageLocation = listBox2.Text;
                   this.pictureBox1.Visible = true;
               }
               else if (extension.ToLower() == ".wmv")
               {
                   this.axWindowsMediaPlayer1.URL = listBox2.Text;
                   this.axWindowsMediaPlayer1.Visible = true;
               }
               else if (extension.ToLower() == ".mpg")
               {
                   this.axWindowsMediaPlayer1.URL = listBox2.Text;
                   this.axWindowsMediaPlayer1.Visible = true;
               }
               else if (extension.ToLower() == ".mpeg")
               {
                   this.axWindowsMediaPlayer1.URL = listBox2.Text;
                   this.axWindowsMediaPlayer1.Visible = true;
               }
               else if (extension.ToLower() == ".avi")
               {
                   this.axWindowsMediaPlayer1.URL = listBox2.Text;
                   this.axWindowsMediaPlayer1.Visible = true;
               }
               else if (extension.ToLower() == ".mpg")
               {
                   this.axWindowsMediaPlayer1.URL = listBox2.Text;
                   this.axWindowsMediaPlayer1.Visible = true;
               }
               else if (extension.ToLower() == ".mov")
               {
                   this.axWindowsMediaPlayer1.URL = listBox2.Text;
                   this.axWindowsMediaPlayer1.Visible = true;
               }
               else if (extension.ToLower() == ".qt")
               {
                   this.axWindowsMediaPlayer1.URL = listBox2.Text;
                   this.axWindowsMediaPlayer1.Visible = true;
               }
           }
       }

       private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
       {

       }

       private void Upload_Click(object sender, EventArgs e)
       {
           foreach (string pathname in listBox2.Items)
           {
               MessageBox.Show("File Location: " + pathname);
           }
       }

       private void button1_Click_1(object sender, EventArgs e)
       {

           if (listBox1.SelectedIndex >= 0)
           {
               listBox2.Items.RemoveAt(listBox1.SelectedIndex);
               listBox1.Items.Remove(listBox1.SelectedItem);
               if (listBox1.Items.Count > 0)
               {
                   listBox1.SelectedIndex = 0;
               }
           }
       }

       private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
       {

       }

       private void pictureBox1_Click(object sender, EventArgs e)
       {

       }

       private void progressBar1_Click(object sender, EventArgs e)
       {

       }

       private void button2_Click(object sender, EventArgs e)
       {
           if(this.pictureBox1.Visible == true)
           {
               Image img = this.pictureBox1.Image;

               Bitmap copy = new Bitmap(img.Width,img.Height);

               ImageAttributes ia = new ImageAttributes();


               ColorMatrix cm = new ColorMatrix();
               cm.Matrix00 = cm.Matrix11 = cm.Matrix22 = 0.99f;
               cm.Matrix33 = cm.Matrix44 = 1;
               cm.Matrix40 = cm.Matrix41 = cm.Matrix42 = .04f;

                               
               ia.SetColorMatrix(cm);

               
               Graphics g = Graphics.FromImage(copy);
               
               g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);


               
               g.Dispose();
               
               img.Dispose();
           }
           
       }
   }
}

Posted

img = this.pictureBox1.Image;

 

Here img is now a reference to the bitmap in the picturebox.

 

img.Dispose();

 

this disposes of the bitmap that img refers to. So pictureBox1.Image is now disposed. When windows tries to paint pictureBox1, it throws an error.

 

Also you draw a modified version of img into the bitmap called copy. But do nothing with it...

 

So what you probably want to do is:

 

g.Dispose();

picturebox1.image = copy;

img.dispose();

 

Now when windows paints, it will draw the bitmap copy, which still exists.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...