nullReferenceException was unhandled error

Started by
2 comments, last by rip-off 12 years, 11 months ago
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;


}


}


}
Advertisement
You never give "newBitmap" a value. It will always be null.
so plz tell me where what should i do i am totally blank now ...



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.

This topic is closed to new replies.

Advertisement