对图像进行操作并会修改图像字节的方法:
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;
}
将图像另存为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);
}
}
被调用的方法:
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);
}
因此,此Dummy方法的输出为:
11111110
11111111
11111111
11111111
11111111
11111111
输出显示,在第二processBytes
期间,该图像与该方法的第一次调用不同。我觉得保存图片一定是有问题的,但是在实现过程中,我遵循了Microsoft网站的示例。非常感谢。