Hi Guys,
I'm working on conversion of colors to Sepia & Black/White. I don't see any error but the output is nothing. The conversion is not happen from original image to Sepia or Black/White .
Somebody please tell me what is the mistake here. Thank you.
Gray-scale or B/W:
private void Button_Click_1(object sender, RoutedEventArgs e) //GrayScale/Black&White
{
string destination_dir = @"C:\Users\krangaraj\Pictures\AAA";
string[] extensions = { ".jpg", ".gif", ".jpeg", ".png", ".tiff" };
string[] dizin = Directory.GetFiles(destination_dir, "*.*")
.Where(f => extensions.Contains(new FileInfo(f).Extension.ToLower())).ToArray();
foreach (string file_name in dizin)
{
Image img = Image.FromFile(file_name);
Bitmap bmp = img as Bitmap;
BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, img.Width, img.Height),
ImageLockMode.ReadWrite, bmp.PixelFormat);
int byteCount = bmpData.Stride*bmpData.Height;
byte[] bytes = new byte[byteCount];
Marshal.Copy(bmpData.Scan0, bytes, 0, byteCount);
bmp.UnlockBits(bmpData);
Bitmap bmpNew = new Bitmap(bmp.Width, bmp.Height);
BitmapData bmpData1 = bmpNew.LockBits(new System.Drawing.Rectangle(new Point(), bmpNew.Size),
ImageLockMode.ReadWrite, bmp.PixelFormat);
Marshal.Copy(bytes, 0, bmpData1.Scan0, bytes.Length);
bmpNew.UnlockBits(bmpData1);
bmp.Dispose();
MakeGrayscale3(bmpNew);
bmpNew.Save(file_name);
}
}
public static Bitmap MakeGrayscale3(Bitmap original)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
//get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
//create the grayscale ColorMatrix
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
//create some image attributes
ImageAttributes attributes = new ImageAttributes();
//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
//draw the original image on the new image
//using the grayscale color matrix
g.DrawImage(original, new System.Drawing.Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
//dispose the Graphics object
g.Dispose();
return newBitmap;
}
Sepia:
private void Button_Click(object sender, RoutedEventArgs e) //Sepia
{
string destination_dir = @"C:\Users\krangaraj\Pictures\AAA";
string[] extensions = { ".jpg", ".gif", ".jpeg", ".png", ".tiff" };
string[] dizin = Directory.GetFiles(destination_dir, "*.*")
.Where(f => extensions.Contains(new FileInfo(f).Extension.ToLower())).ToArray();
foreach (string file_name in dizin)
{
Image img = Image.FromFile(file_name);
Bitmap bmp = img as Bitmap;
BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
int byteCount = bmpData.Stride * bmpData.Height;
byte[] bytes = new byte[byteCount];
Marshal.Copy(bmpData.Scan0, bytes, 0, byteCount);
bmp.UnlockBits(bmpData);
Bitmap bmpNew = new Bitmap(bmp.Width, bmp.Height);
BitmapData bmpData1 = bmpNew.LockBits(new System.Drawing.Rectangle(new Point(), bmpNew.Size), ImageLockMode.ReadWrite, bmp.PixelFormat);
Marshal.Copy(bytes, 0, bmpData1.Scan0, bytes.Length);
bmpNew.UnlockBits(bmpData1);
bmp.Dispose();
DrawAsSepiaTone(bmpNew);
bmpNew.Save(file_name);
}
}