Sign in to follow this  
chillimilli

nullReferenceException was unhandled error

Recommended Posts

i wrote this piece of code , it opens an image and converts it to other formats but its gamma correction is not working and giving an error nullReferenceException was unhandled at this line
Bitmap bm = new Bitmap(newBitmap.Width, newBitmap.Height);
plz help me i am new to c#and i have to submit my task :((


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

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{



Image file;
Boolean opened = false;
float gamma = 0;

Bitmap newBitmap;




public Form1()
{
InitializeComponent();
}



private void toolStripButton1_Click(object sender, EventArgs e)
{
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
file = Image.FromFile(openFileDialog1.FileName);
pictureBox1.Image = file;
opened = true;

}
}

private void toolStripButton2_Click(object sender, EventArgs e)
{
DialogResult dr = saveFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
if (opened)
{

if (saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length - 3).ToLower() == "bmp")
{
file.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
}
if (saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length - 3).ToLower() == "jpg")
{
file.Save(saveFileDialog1.FileName, ImageFormat.Jpeg);
}
if (saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length - 3).ToLower() == "png")
{
file.Save(saveFileDialog1.FileName, ImageFormat.Png);
}
if (saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length - 3).ToLower() == "gif")
{
file.Save(saveFileDialog1.FileName, ImageFormat.Gif);
}
else {
MessageBox.Show("You need to open an image first !");

}
}
}
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void trackBar1_Scroll(object sender, EventArgs e)
{


label1.Text = trackBar1.Value.ToString();
gamma = 0.04f * trackBar1.Value;
Bitmap bm = new Bitmap(newBitmap.Width, newBitmap.Height);
Graphics g = Graphics.FromImage(bm);
ImageAttributes ia = new ImageAttributes();
ia.SetGamma(gamma);
g.DrawImage(newBitmap, new Rectangle(0, 0, newBitmap.Width, newBitmap.Height), 0, 0, newBitmap.Width, newBitmap.Height, GraphicsUnit.Pixel, ia);
g.Dispose();
ia.Dispose();
pictureBox1.Image = bm;


}


}


}

Share this post


Link to post
Share on other sites
I don't know the C# API's very well, but some light Googling suggests that if you remove the "newBitmap" field and replace all references to it with references to "image" it could work (or at least get further than it is at the moment). Alternatively you can keep the two-image solution, leave "image" as the unmodified image from the file and using a "displayImage" which is a copy of that image with any current modifications done to it.

Either way you should probably insert a check to ensure that image is not-null before working on it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this