Method that operates on image and will modify image's bytes:
public BitmapSource processBytes(String filePath, int step, String param2)
{
BitmapSource source = JpegBitmapDecoder.Create(new Uri(filePath), BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames[0];
//BitmapImage source = new BitmapImage(new Uri(filePath));
int bytesPerPixel = (source.Format.BitsPerPixel + 7) / 8;
int stride = bytesPerPixel * source.PixelWidth;
byte[] buffer = new byte[stride * source.PixelHeight];
source.CopyPixels(buffer, stride, 0);
Console.WriteLine(Convert.ToString(buffer[1],2).PadLeft(8, '0'));
Console.WriteLine(Convert.ToString(buffer[2],2).PadLeft(8, '0'));
Console.WriteLine(Convert.ToString(buffer[3],2).PadLeft(8, '0'));
var bitMapSource = BitmapSource.Create(source.PixelWidth, source.PixelHeight,
source.DpiX, source.DpiY, source.Format, null, buffer, stride);
return bitMapSource;
}
Function that saves an image as jpg:
private void saveImage(string path, BitmapSource content)
{
using (var fileStream = new FileStream(path, FileMode.Create))
{
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(content));
encoder.Save(fileStream);
}
}
Method that is invoked:
private void Dummy(object sender, RoutedEventArgs e)
{
String filepath = "...\\small.jpg";
String filepath2 = "...\\small2.jpg";
String filepath3 = "...\\small3.jpg";
ImageFileHandler fileHandler = new ImageFileHandler(new Dummy(), "");
BitmapSource output = fileHandler.signWithWatermark(filepath, 1, "");
saveImage(filepath2, output);
BitmapSource output2 = fileHandler.signWithWatermark(filepath2, 1, "");
saveImage(filepath3, output2);
}
So the output of this Dummy method is:
11111110
11111111
11111111
11111111
11111111
11111111
The output shows that during second processBytes
the image differ from the first invocation of the method. I feel like it has to be issue with saving the image, but during implmenetation I followed an example from Microsoft site. Help much appreciated.