• Advertisement
Sign in to follow this  

nullReferenceException was unhandled error

This topic is 2439 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Well, you are using the width and height of newBitmap without it having been initialized.
newBitmap is null.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement