Click here to Skip to main content
15,897,891 members
Home / Discussions / C#
   

C#

 
QuestionCrystal Reports? Pin
Marix26-Sep-03 2:58
Marix26-Sep-03 2:58 
AnswerRe: Crystal Reports? Pin
Member 56585726-Sep-03 8:00
Member 56585726-Sep-03 8:00 
GeneralRe: Crystal Reports? Pin
Member 56585726-Sep-03 8:03
Member 56585726-Sep-03 8:03 
GeneralRe: Crystal Reports? Pin
Marix28-Sep-03 21:58
Marix28-Sep-03 21:58 
Questionhow to speed up GDI+ without using pointer Pin
zecodela25-Sep-03 23:34
zecodela25-Sep-03 23:34 
AnswerRe: how to speed up GDI+ without using pointer Pin
Anonymous26-Sep-03 1:59
Anonymous26-Sep-03 1:59 
GeneralRe: how to speed up GDI+ without using pointer Pin
fadee26-Sep-03 2:02
fadee26-Sep-03 2:02 
AnswerRe: how to speed up GDI+ without using pointer Pin
fadee26-Sep-03 4:57
fadee26-Sep-03 4:57 
Here is the code... Instead of making Bitmap object, make object of this class, constructors are more then enough Smile | :)

I claim that it VERY FAST as compared to GDI of .NET... the operation done by GDI in 9 seconds will be done in 3 seconds.. with same ease!

Don foget to compile with /unsafe switch...

A structure me using...

<br />
using System;<br />
<br />
namespace ImageProcessor.Imaging<br />
{<br />
	/// <summary><br />
	/// This structure contains structure information to be used in Facer.Imaging.BitmapImage.FaceRImage Class.<br />
	/// </summary><br />
	unsafe public struct PixelData<br />
	{<br />
		public byte Red;<br />
		public byte Green;<br />
		public byte Blue;<br />
	}<br />
}<br />


THE CLASS

<br />
using System;<br />
using System.Drawing;<br />
using System.Drawing.Imaging;<br />
using System.Drawing.Drawing2D;<br />
<br />
namespace ImageProcessor.Imaging<br />
{<br />
	/// <summary><br />
	/// Summary description for BitmapImage.<br />
	/// </summary><br />
<br />
	public class Image<br />
	{<br />
		private String File;<br />
<br />
		public String FileName<br />
		{<br />
			get<br />
			{<br />
				return File;<br />
			}<br />
		}<br />
		<br />
		private int H;<br />
		private int W;<br />
<br />
		public int Height<br />
		{<br />
			get<br />
			{<br />
				return H;<br />
			}<br />
		}<br />
<br />
		public int Width<br />
		{<br />
			get<br />
			{<br />
				return W;<br />
			}<br />
		}<br />
<br />
		private Bitmap img;<br />
		public Bitmap BitmapImage<br />
		{<br />
			get<br />
			{<br />
				return img;<br />
			}<br />
		}<br />
<br />
		private BitmapData Data;<br />
		<br />
		unsafe private Byte* PBase = null;<br />
		private int ByteWidth = 0;<br />
		Rectangle Bounds;<br />
<br />
		private Image()<br />
		{<br />
		}<br />
<br />
<br />
		unsafe public Image(String Path)<br />
		{<br />
			File = Path;<br />
			img = new Bitmap(File);<br />
<br />
			if(img.PixelFormat != PixelFormat.Format24bppRgb)<br />
				throw new ImageProcessor.Exceptions.Imaging.InvalidImageFormatException("Invalid Image Format. Expecting: Format24bppRgb Provided: " + img.PixelFormat.ToString());<br />
<br />
			Data = null;<br />
<br />
			H = this.img.Height;<br />
			W = this.img.Width;<br />
<br />
			GraphicsUnit Unit = GraphicsUnit.Pixel;<br />
			RectangleF BoundsF = img.GetBounds(ref Unit);<br />
<br />
			Bounds = new Rectangle((int) BoundsF.X,(int) BoundsF.Y,(int) BoundsF.Width,(int) BoundsF.Height);<br />
			ByteWidth = (int) BoundsF.Width * sizeof(PixelData);<br />
<br />
			if (ByteWidth % 4 != 0)<br />
				ByteWidth = 4 * (ByteWidth / 4 + 1);<br />
		}<br />
<br />
<br />
		unsafe public Image(int w,int h)<br />
		{<br />
			File = this.GetHashCode().ToString() + ".bmp";<br />
			img = new Bitmap(w,h,PixelFormat.Format24bppRgb);<br />
			Data = null;<br />
<br />
			H = this.img.Height;<br />
			W = this.img.Width;<br />
<br />
			GraphicsUnit Unit = GraphicsUnit.Pixel;<br />
			RectangleF BoundsF = img.GetBounds(ref Unit);<br />
<br />
			Bounds = new Rectangle((int) BoundsF.X,(int) BoundsF.Y,(int) BoundsF.Width,(int) BoundsF.Height);<br />
			ByteWidth = (int) BoundsF.Width * sizeof(PixelData);<br />
<br />
			if (ByteWidth % 4 != 0)<br />
				ByteWidth = 4 * (ByteWidth / 4 + 1);<br />
		}<br />
<br />
<br />
		unsafe public Image(Bitmap bmp)<br />
		{<br />
			File = this.GetHashCode().ToString() + ".bmp";<br />
			img = bmp;<br />
			Data = null;<br />
<br />
			H = this.img.Height;<br />
			W = this.img.Width;<br />
<br />
			GraphicsUnit Unit = GraphicsUnit.Pixel;<br />
			RectangleF BoundsF = img.GetBounds(ref Unit);<br />
<br />
			Bounds = new Rectangle((int) BoundsF.X,(int) BoundsF.Y,(int) BoundsF.Width,(int) BoundsF.Height);<br />
			ByteWidth = (int) BoundsF.Width * sizeof(PixelData);<br />
<br />
			if (ByteWidth % 4 != 0)<br />
				ByteWidth = 4 * (ByteWidth / 4 + 1);<br />
		}<br />
<br />
<br />
		unsafe public Color GetPixelAt(int x,int y)<br />
		{<br />
			this.LockImage();<br />
			PixelData *temp = (PixelData*) (PBase + y * ByteWidth + x * sizeof(PixelData));<br />
			this.UnlockImage();<br />
			<br />
			return Color.FromArgb(temp->Red,temp->Green,temp->Blue);<br />
		}<br />
<br />
<br />
		unsafe public Color GetPixelAtChecked(int x,int y)<br />
		{<br />
			if(x >= img.Width || y >= img.Height)<br />
				throw new ImageProcessor.Exceptions.Imaging.ImageIndexOutOfBoundsException(y + " <- " +  img.Height);<br />
<br />
			this.LockImage();<br />
			PixelData *temp = (PixelData*) (PBase + y * ByteWidth + x * sizeof(PixelData));<br />
			this.UnlockImage();<br />
<br />
			return Color.FromArgb(temp->Red,temp->Green,temp->Blue);<br />
		}<br />
<br />
<br />
		unsafe public void SetPixelAt(int x,int y,byte Red,byte Green,byte Blue)<br />
		{<br />
			this.LockImage();<br />
			PixelData *temp = (PixelData*) (PBase + y * ByteWidth + x * sizeof(PixelData));<br />
			temp->Red = Red;<br />
			temp->Green = Green;<br />
			temp->Blue = Blue;<br />
			this.UnlockImage();<br />
		}<br />
<br />
<br />
		unsafe public void SetPixelAt(int x,int y,Color col)<br />
		{<br />
			this.LockImage();<br />
			<br />
			PixelData *temp = (PixelData*) (PBase + y * ByteWidth + x * sizeof(PixelData));<br />
			temp->Red = col.R;<br />
			temp->Green = col.G;<br />
			temp->Blue = col.B;<br />
<br />
			this.UnlockImage();<br />
		}<br />
<br />
<br />
		unsafe public void SetPixelAtChecked(int x,int y,byte Red,byte Green,byte Blue)<br />
		{<br />
			if(x >= img.Width || y >= img.Height)<br />
				throw new ImageProcessor.Exceptions.Imaging.ImageIndexOutOfBoundsException();<br />
<br />
			this.LockImage();<br />
			PixelData *temp = (PixelData*) (PBase + y * ByteWidth + x * sizeof(PixelData));<br />
			temp->Red = Red;<br />
			temp->Green = Green;<br />
			temp->Blue = Blue;<br />
			this.UnlockImage();<br />
		}<br />
<br />
<br />
		unsafe private void LockImage()<br />
		{<br />
			Data = img.LockBits(Bounds, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);<br />
			PBase = (Byte*) Data.Scan0.ToPointer();<br />
		}<br />
<br />
<br />
		unsafe private void UnlockImage()<br />
		{<br />
			img.UnlockBits(Data);<br />
			Data = null;<br />
			PBase = null;<br />
		}<br />
<br />
<br />
		protected void Finalize()<br />
		{<br />
			img.Dispose();<br />
		}<br />
<br />
<br />
		unsafe public Image Clone()<br />
		{<br />
			Image temp = new Image();<br />
			<br />
			temp.File = this.File;<br />
			temp.img = (Bitmap) this.img.Clone();<br />
<br />
			temp.Data = null;<br />
<br />
			temp.H = temp.img.Height;<br />
			temp.W = temp.img.Width;<br />
<br />
			GraphicsUnit Unit = GraphicsUnit.Pixel;<br />
			RectangleF BoundsF = img.GetBounds(ref Unit);<br />
<br />
			temp.Bounds = new Rectangle((int) BoundsF.X,(int) BoundsF.Y,(int) BoundsF.Width,(int) BoundsF.Height);<br />
			temp.ByteWidth = (int) BoundsF.Width * sizeof(PixelData);<br />
<br />
			if (temp.ByteWidth % 4 != 0)<br />
				temp.ByteWidth = 4 * (temp.ByteWidth / 4 + 1);<br />
<br />
			return temp;<br />
		}<br />
<br />
<br />
		public void SaveImageAs(String FileName)<br />
		{<br />
			this.img.Save(FileName);<br />
		}<br />
<br />
		public void ConvertToGreyScale()<br />
		{<br />
			byte r;<br />
			Color c;<br />
<br />
			for(int y = 0; y < this.img.Height; y++)<br />
			{<br />
				for(int x = 0; x < this.img.Width; x++)<br />
				{<br />
					c = this.GetPixelAt(x,y);<br />
					r = (byte)((c.R + c.G + c.B) / 3);<br />
					this.SetPixelAt(x,y,r,r,r);<br />
				}<br />
			}<br />
		}<br />
<br />
		unsafe public char[] GetBytes()<br />
		{<br />
			char[] temp = new char[this.Height * this.Width * 3];<br />
			PixelData *Pixel;<br />
			int index = 0;<br />
<br />
			Data = img.LockBits(Bounds, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);<br />
			PBase = (Byte*) Data.Scan0.ToPointer();<br />
<br />
			for(int y = 0; y < this.Height; y++)<br />
			{<br />
				for(int x = 0; x < this.Width; x++)<br />
				{<br />
					Pixel = (PixelData*) (PBase + y * ByteWidth + x * sizeof(PixelData));<br />
					temp[index] = (char) Pixel->Red;<br />
					temp[index + 1] = (char) Pixel->Green;<br />
					temp[index + 2] =(char) Pixel->Blue;<br />
					index = index + 3;<br />
				}<br />
			}<br />
<br />
			img.UnlockBits(Data);<br />
			Data = null;<br />
			PBase = null;<br />
<br />
			return temp;<br />
		}<br />
<br />
<br />
		unsafe public static Image BuildImage(int w, int h, char[] data)<br />
		{<br />
			if(data.Length != (w*h*3))<br />
				throw new Exception("Process of Building Image Failed. Invalid Data Byte Array.");<br />
<br />
			Image temp = new Image(w,h);<br />
			int index = 0;<br />
			PixelData *Pixel;<br />
			<br />
			temp.Data = temp.img.LockBits(temp.Bounds, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);<br />
			temp.PBase = (Byte*) temp.Data.Scan0.ToPointer();<br />
<br />
			for(int y = 0; y < h; y++)<br />
			{<br />
				for(int x = 0; x < w; x++)<br />
				{<br />
					Pixel = (PixelData*) (temp.PBase + y * temp.ByteWidth + x * sizeof(PixelData));<br />
<br />
					Pixel->Red = (byte) data[index];<br />
					Pixel->Green = (byte) data[index + 1];<br />
					Pixel->Blue = (byte) data[index + 2];<br />
					index = index + 3;<br />
				}<br />
			}<br />
<br />
			temp.img.UnlockBits(temp.Data);<br />
			temp.Data = null;<br />
			temp.PBase = null;<br />
<br />
			return temp;<br />
		}<br />
<br />
<br />
		unsafe static public Image XOR(Image NewFrame,Image PreviousFrame)<br />
		{<br />
			bool NullTest = true;<br />
<br />
			if(NewFrame.Height != PreviousFrame.Height && NewFrame.Width != PreviousFrame.Width)<br />
				throw new Exception("Process of XORing Image Failed. Invalid Data Byte Array.");<br />
<br />
			Image img = new Image(NewFrame.Width,NewFrame.Height);<br />
			PixelData *Pixel1;<br />
			PixelData *Pixel2;<br />
			<br />
			NewFrame.Data = NewFrame.img.LockBits(NewFrame.Bounds, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);<br />
			NewFrame.PBase = (Byte*) NewFrame.Data.Scan0.ToPointer();<br />
<br />
			PreviousFrame.Data = PreviousFrame.img.LockBits(PreviousFrame.Bounds, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);<br />
			PreviousFrame.PBase = (Byte*) PreviousFrame.Data.Scan0.ToPointer();<br />
<br />
			for(int y = 0; y < NewFrame.Height; y++)<br />
			{<br />
				for(int x = 0; x < img.Width; x++)<br />
				{<br />
					Pixel1 = (PixelData*) (NewFrame.PBase + y * NewFrame.ByteWidth + x * sizeof(PixelData));<br />
					Pixel2 = (PixelData*) (PreviousFrame.PBase + y * PreviousFrame.ByteWidth + x * sizeof(PixelData));<br />
<br />
					if(Pixel1->Red == Pixel2->Red && Pixel1->Green == Pixel2->Green && Pixel1->Blue == Pixel2->Blue)<br />
						img.SetPixelAt(x,y,Color.White.R,Color.White.G,Color.White.B);<br />
					else<br />
					{<br />
						img.SetPixelAt(x,y,Color.Black.R,Color.Black.G,Color.Black.B);<br />
						NullTest = false;<br />
					}<br />
				}<br />
			}<br />
<br />
			NewFrame.img.UnlockBits(NewFrame.Data);<br />
			NewFrame.Data = null;<br />
			NewFrame.PBase = null;<br />
<br />
			PreviousFrame.img.UnlockBits(PreviousFrame.Data);<br />
			PreviousFrame.Data = null;<br />
			PreviousFrame.PBase = null;<br />
			<br />
			if(NullTest)<br />
				return null;<br />
			else<br />
				return img;<br />
		}<br />
<br />
		public Image ScaleImage(int width, int height)<br />
		{<br />
            Bitmap result = new Bitmap(width,height);<br />
<br />
			Graphics bmpGraphics = Graphics.FromImage(result);<br />
			bmpGraphics.InterpolationMode = InterpolationMode.High;<br />
			bmpGraphics.SmoothingMode = SmoothingMode.AntiAlias;<br />
<br />
			bmpGraphics.DrawImage(this.img, new Rectangle(0, 0, width, height));<br />
<br />
			return new Image(result);<br />
		}<br />
	}<br />
}<br />


---------------------
A gasp of breath,
A sudden death:
The tale begun.

A rustled page
Passes an age:
The tale is done.
GeneralRe: how to speed up GDI+ without using pointer Pin
zecodela26-Sep-03 5:06
zecodela26-Sep-03 5:06 
GeneralRe: how to speed up GDI+ without using pointer Pin
fadee26-Sep-03 6:29
fadee26-Sep-03 6:29 
GeneralRe: how to speed up GDI+ without using pointer Pin
ralfoide26-Sep-03 8:34
ralfoide26-Sep-03 8:34 
GeneralRe: how to speed up GDI+ without using pointer Pin
zecodela26-Sep-03 15:49
zecodela26-Sep-03 15:49 
GeneralRe: how to speed up GDI+ without using pointer Pin
zecodela26-Sep-03 15:51
zecodela26-Sep-03 15:51 
GeneralRe: how to speed up GDI+ without using pointer Pin
zecodela26-Sep-03 19:02
zecodela26-Sep-03 19:02 
GeneralRe: how to speed up GDI+ without using pointer Pin
fadee26-Sep-03 20:00
fadee26-Sep-03 20:00 
GeneralAding a dynamic web reference at runtime Pin
Obi725-Sep-03 22:10
Obi725-Sep-03 22:10 
GeneralRe: Ading a dynamic web reference at runtime Pin
Kannan Kalyanaraman26-Sep-03 0:57
Kannan Kalyanaraman26-Sep-03 0:57 
GeneralRe: Ading a dynamic web reference at runtime Pin
Obi726-Sep-03 1:32
Obi726-Sep-03 1:32 
GeneralSetup creation problem Pin
..Hubert..25-Sep-03 22:03
..Hubert..25-Sep-03 22:03 
GeneralQuestion about sockets. Pin
jtmtv1825-Sep-03 20:08
jtmtv1825-Sep-03 20:08 
QuestionHow to blit a System.Drawing.Image into DirectDraw.Surface ? Pin
ralfoide25-Sep-03 19:06
ralfoide25-Sep-03 19:06 
AnswerRe: How to blit a System.Drawing.Image into DirectDraw.Surface ? Pin
Jeremy Kimball25-Sep-03 20:06
Jeremy Kimball25-Sep-03 20:06 
GeneralRe: How to blit a System.Drawing.Image into DirectDraw.Surface ? Pin
ralfoide26-Sep-03 8:38
ralfoide26-Sep-03 8:38 
GeneralRichTextBox Auto Scroll Down Pin
zecodela25-Sep-03 18:27
zecodela25-Sep-03 18:27 
GeneralRe: RichTextBox Auto Scroll Down Pin
jtmtv1825-Sep-03 20:00
jtmtv1825-Sep-03 20:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.