0

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.

ogarogar
  • 323
  • 2
  • 14
  • What is the exact purpose of `loadAndModify` and `processBytes`? If you want to modify pixel data, use a WriteableBitmap. To change the pixel format, use a FormatConvertedBitmap. Remove all that WinForms stuff. – Clemens Dec 14 '19 at 10:35
  • `loadAndModify` was created to get from `string filepath` an array of bytes, to make modifications on. WIll I be able to work on `byte[]` when using WriteableBitmap? Can you provide me URL to some example how should it be done? – ogarogar Dec 14 '19 at 10:39
  • You already have `BitmapImage bitmapImage`, which is a BitmapSource, so you can call one of its CopyPixels methods overloads to get a pixel buffer. See e.g. here: https://stackoverflow.com/a/39189895/1136211 – Clemens Dec 14 '19 at 10:42
  • @Clemens so I used your link and upadted the question. Function look a lot better, but sadly the same issue. 3 first bytes during first iteration of it are 11111111, 11111110, 11111111. Then I save the image, reopen it and I get `11111110, 11111110, 11111110`. Any ideas was is wrong? – ogarogar Dec 14 '19 at 13:11
  • So you mean even without calling `proccessBytes` the first three bytes in the pixel buffer of the loaded image are different from those of the saved image, but all other pixels are identical? Please try to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Do not show irrelevant parts, like how you use OpenFileDialog etc. – Clemens Dec 14 '19 at 13:30
  • @Clemens ok, I restructed my question. Please check it out now. – ogarogar Dec 14 '19 at 13:57
  • Sorry, but what are the `ImageFileHandler` and `Dummy` classes? And how is "*the output of this Dummy method*" generated? The only Dummy method you are showing is the event handler, without any output. Seriously, this makes no sense. – Clemens Dec 14 '19 at 14:04
  • Perhaps also worth mentioning, `buffer[1]` is not the first byte in buffer. That would by `buffer[0]`. – Clemens Dec 14 '19 at 14:08
  • My bad... I changed code little bit to make it more "minimal reproducible example". this Dummy class will be some class that I implement byte modifications in. But for now its just some class with empty method (with is not invoked !). About buffer[1] and buffer[0], again you are right. Again, my bad. Nevertheless I only use it to check is there any difference. There is one and there should be none. – ogarogar Dec 14 '19 at 14:11
  • I changed the way I save a file. Now I use `PngBitmapEncoder` and i think everything is OK now... Is it possible that it was encoder's fault? – ogarogar Dec 14 '19 at 14:13
  • 1
    The difference is that PNG encoding uses a lossless compression, whil JPEG encoding loses quality, which may of course result in slighty changed pixel values. – Clemens Dec 14 '19 at 14:18

0 Answers0