C# .NET - save a image in jpeg format - Asked By john santhosh on 07-Dec-05 02:11 AM

Hi

Using Microsoft.ink...I have to draw a image in the form and savethat image in .jpeg format.....So i haveto install Microsoft Tablet pc.....
so finally I installed ... and i did a program with 2 forms namely save and exit......
so no when i run this program..i can able to draw a image of my own in the form..now when i click save button...i should be able to save the image in .jpeg format.. BUT I couldnt save.. when i click ,save..and give a name for teh image .... an error is shown...

ERROR MESSAGE:
System.NullReferenceException: Object reference not set to an instance of an object.
   at InkImage.Form1.button2_Click(Object sender, EventArgs e) in d:\inkimage\form1.cs:line 144..
ie, error in the last line of this program [ DrawArea.Save( sfd.FileName, format );]


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Ink;
using System.IO;


namespace InkImage
{
	public class Form1 : System.Windows.Forms.Form
	{
		
		// Declare the Ink Collector object
		private InkCollector myInkCollector;
		private const float ThinInkWidth = 50;
		

		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;

		
		private Bitmap DrawArea;// make a persistent drawing area

		
		private void Form1_Load(object sender, System.EventArgs e)
		{
	// Create a new ink collector and assign it to this form's window
	               	myInkCollector = new InkCollector(this.Handle);

	// Set the pen width to be a width
		myInkCollector.DefaultDrawingAttributes.Width = ThinInkWidth;

	// Turn the ink collector on
	                myInkCollector.Enabled = true;   
		
		}

//EXIT BUTTON		

                              private void button1_Click(object sender, System.EventArgs e)
		{
			myInkCollector.Enabled = false;
			this.Dispose();
		}
		
//SAVE BUTTON
		private void button2_Click(object sender, System.EventArgs e)
		{
			ImageFormat format = ImageFormat.Jpeg;
			SaveFileDialog sfd = new SaveFileDialog();
			sfd.Filter = "JPEG Files(*.jpg)|*.jpg";

			if (sfd.ShowDialog()  == DialogResult.OK)
			{
		              // now save the image in the DrawArea
			DrawArea.Save( sfd.FileName, format );
			}

			
		}
	}
}


please help me to get the solution......

Dhol

Would this help? - Asked By Jerry Laudat on 07-Dec-05 02:20 PM

I'll make a wild guess at it. I'm at work and don't have access right now to the C# IDE, plus I'm a newbie, but maybe I can get you on the right track.

It was my impression that the standard way of drawing on a form is to make use of the GDI graphics object which paints every form. This graphics object is part of the event object "e" passed by dot net into the form1_paint event, something like this (if i can recall)

Form1_Paint(Object sender, PaintEventArgs e)
{
Here you can make use of the graphics object to draw on the form. For example
	Font f = new Font("New Times Roman", 24)
	e.Graphics.DrawString("This is my form", brushes.black, 0, 0)  //0,0 is location x,y to start drawing 
}
The graphics object is a drawing surface. Your comomands such as DrawString draw on the surface, and then dotnet transfers the image from the surface onto the form at the end of the paint event.  However, you don't have to put the commands in the block above. You can send the Graphics object (the drawing surface) to a separate sub (call it DrawingSub) to do the drawing in that sub using code seomthing like this
	Dim DrawArea as Graphics = e.Graphics
	DrawingSub(DrawArea) 'passes drawing surface to a drawing sub
and then
	Private Sub DrawingSub(Graphics DrawingArea)
		DrawingArea.DrawString("This is my form")
	End sub

The advantange of doing it this way is that it saves code if you want to save this image. Elsewhere you can create a second drawing surface bound to a bitmpa, and pass it into the DrawingSub above
              Dim Bitmpa1 As New Bitmap(850, 650, Imaging.PixelFormat.Format32bppArgb)
       Dim DrawArea2 As Graphics = Graphics.FromImage(EobImage)
	 Call DrawingSub(DrawingArea2) 'draws the same stuff on the bitmap
	 Bitmap1.Save("C:myImage.Tif", ImageFormat1.Tiff)
But since dotNet probably does some default drawing on a Form, I can't guarantee (havent tried it) that your saved image will have EVERYTHIGN that the form has. Well, hope this helped anyway.

using microsoft Ink, draw a image ,save as gif - Asked By john santhosh on 09-Dec-05 08:01 AM

Hi

I have done the program..ie, I can able to draw a image and save as a .gif image now...Now I want one more thing to be done ie,..... I use Crystal Reports XI... here in crystal reports we can load a image at runtime using c#..... 

Now wht I have to do is that add a crystal report viewer to the form...of teh previous program and add a Button named Browse... so that now when the program is run.. as usual we have to draw a image and save it as .gif image..then now by clicking browse button...we should load that saved image and display it in crystal report.... 
for this we have to design a xml schema and add a field... ie..for gif file..... and we haveto take that .xml sxhema as teh database for the crytsal report....I have done this program...but i can ableto show any image in crytsal reprot but except teh saved image.....

I will paste my coding part along with teh program. Please have a look at it and solve my problem..

using System;
using Microsoft.Ink;
using System.IO;


		
		// Declare the Ink Collector object
		private InkCollector myInkCollector;
		private const float ThinInkWidth = 50;
		//exit button
		private System.Windows.Forms.Button button1;
                                //save button
		private System.Windows.Forms.Button button2;
		private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
                                //browse button
		private System.Windows.Forms.Button button3;



                                 // Prcocedure: AddImageRow
		//       reads an image file and adds this image to a dataset table
		//
		//   [in]    tbl         DataTable
				
                                 void AddImageRow(DataTable tbl, string name, string filename)
		{
		     FileStream fs = new FileStream(filename, FileMode.Open);// create a file stream
		     BinaryReader br = new BinaryReader(fs);  	// create binary reader
			DataRow row; 

			// create a new datarow
			row = tbl.NewRow();

			// set name field and image field
			row[0] = name;
			row[1] = br.ReadBytes((int)br.BaseStream.Length);

			// add this row to the table
			tbl.Rows.Add(row);

			// clean up
			br = null;
			fs = null;
		}




		private void Form1_Load(object sender, System.EventArgs e)
		{
			// Create a new ink collector and assign it to this form's window
			myInkCollector = new InkCollector(this.Handle);
			// Set the pen width to be a width
			myInkCollector.DefaultDrawingAttributes.Width = ThinInkWidth;
			// Turn the ink collector on
			myInkCollector.Enabled = true;   
		}
		


		//Exit Button
		private void button1_Click(object sender, System.EventArgs e)
		{
			// Turn the ink collector Off
			myInkCollector.Enabled = false;
			this.Dispose();
		}
		



		//Save Button
		private void button2_Click(object sender, System.EventArgs e)
		{
			FileStream gifFile;
			byte[]fortifiedGif = null;
			// Create a directory to store the fortified GIF which also contains ISF 
			// and open the file for writing 
			Directory.CreateDirectory("c:\\Images");
			gifFile = File.OpenWrite("c:\\Images\\display.gif");
			// Generate the fortified GIF represenation of the ink 
			fortifiedGif = myInkCollector.Ink.Save(PersistenceFormat.Gif);
			// Write and close the gif file 
			gifFile.Write(fortifiedGif,0,fortifiedGif.Length);
			gifFile.Close();
			
		}



                                //Browse Buttton   
		private void button3_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog openFileDialog1 = new OpenFileDialog();
			openFileDialog1.Filter = "Image Files(*.gif) |*.gif | All Files(*.*) | *.*";
			openFileDialog1.ShowDialog(this);
			//the variable myPic contains the string of the full File Name,it includes the full path. 
			string mypic = openFileDialog1.FileName;


			DataSet data = new DataSet();

			// add a table 'Images' to the dataset
			data.Tables.Add("Images");

			// add two fields
			data.Tables[0].Columns.Add("Country", System.Type.GetType("System.String"));
			data.Tables[0].Columns.Add("img", System.Type.GetType("System.Byte[]"));
			
			AddImageRow(data.Tables[0],mypic,mypic);

			// create a report
			showingimage cr = new showingimage();
			cr.SetDataSource(data);
			// pass a reportdocument to the viewer
			crystalReportViewer1.ReportSource = cr;
		}


this is my coding part..please solve it ..

how to save a compressed image in jpeg format - viishal ganjoo replied to john santhosh on 16-May-09 02:01 AM

i am doing my project on omage compression... we are working on jpeg compression method.... it involves right from subsampling to the huffman coding.... my question is after i do huffnman coding how do i save an image in jpeg format..... as image consista of three matrix y,cb,cr..... how should i save an image making use of these three matrix...... plz anyone help us

                                                             vishal