0% found this document useful (0 votes)
29 views

Intro

The document discusses different methods for reading, writing, and manipulating pixel data in images using C#. It covers loading and saving images, getting and setting individual pixel colors, and locking pixel bits for bulk pixel access. It also discusses displaying loaded images on screen for viewing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Intro

The document discusses different methods for reading, writing, and manipulating pixel data in images using C#. It covers loading and saving images, getting and setting individual pixel colors, and locking pixel bits for bulk pixel access. It also discusses displaying loaded images on screen for viewing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Multimedia-Lecture-One

INTRODUCTION
TO IMAGE USING
C#

Eng. Noor AL-Hakim


Digital Image Representation
Dealing With Image using
C#

o Reading and Writing Image


o Reading and Writing Pixel
o GetPixel and SetPixel Method
o LockBits Method
o Displaying the Image
Reading and Writing Image

Bitmap represents an image Reading


as a rectangular grid of pixels // Load the image file
string imagePath = "input.jpg";
and provides methods and Bitmap image = new Bitmap(imagePath);
properties for manipulating
and working with images.
Writing
// Save the image to a file
string outputPath = "output.jpg";
image.Save(outputPath);
Reading Pixel using GetPixel Method

Bitmap image = new Bitmap("image.jpg");

Color GetPixel(int x, int y): Color pixelColor = image.GetPixel(100, 100);


Retrieves the color of the pixel at
the specified coordinates(x, y) in Console.WriteLine($"Pixel Color at (100, 100):
R={pixelColor.R}, G={pixelColor.G}, B={pixelColor.B}");
the bitmap image
Writing Pixel using SetPixel Method

Bitmap image = new Bitmap("image.jpg");

Color newColor = Color.Red;


void SetPixel(int x, int y, Color color):
Sets the color of the pixel at the image.SetPixel(100, 100, newColor);
specified coordinates (x, y) in the
image.Save("modified_image.jpg");
bitmap image.

Try it using a large


image for more than
one pixel, what do you
notice?!
Reading and Writing Pixel using LockBits Method

// Load an image
Bitmap bitmap = new Bitmap("image.jpg");

// Lock the bitmap to access pixel data


BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly, bitmap.PixelFormat);

// pixel manipulation (Do something)

// Unlock the bitmap


bitmap.UnlockBits(bitmapData);
Reading and Writing Pixel using LockBits Method

// pixel manipulation (Example)

// Get the address of the first line.


IntPtr ptr = bmpData.Scan0;

// Declare an array to hold the bytes of the bitmap.


int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
byte[] rgbValues = new byte[bytes];

// Copy the RGB values into the array.


System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
Reading and Writing Pixel using LockBits Method

// pixel manipulation

// Set every third value to 255. A 24bpp bitmap will look red.
for (int counter = 2; counter < rgbValues.Length; counter += 3)
rgbValues[counter] = 255;
// Copy the RGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
“ Which of the two methods
do you think is better, and
why? "
Displaying the Image

Form form = new Form();


form.Size = new Size(image.Width,image.Height);

// Handle the Paint event of the form


form.Paint += (sender, e) =>
{
// Get the graphics object
Graphics g = e.Graphics;

// Draw the image at position (0, 0)


g.DrawImage(image, new Point(0, 0));
};

// Show the form


Application.Run(form);
Displaying the Image

// Specify the path to the image file


string imagePath = "image.jpg";

// Open the image file using the default image viewer


Process.Start(imagePath);
You Should 1. Emgu CV Library to handle with image and video files.
2. NAudio to handle with audio files.
Download ! 3. MathNet.Numerics to handle with Fourier transformation.
That’s All

You might also like