在C#中将页脚/ EXIF(二进制数据)添加到JPG图像

时间:2015-02-11 11:40:25

标签: c# .net jpeg .net-4.5

大家好,我有一个图像,我想将二进制数据作为页脚添加到该图像。

RGBImage rgbImage =  (RGBImage) RGBImage.LoadImage(@"test.tiff");
byte[] bytes = File.ReadAllBytes(@"C:\TEMP\gili.bin");
int padding =(int) Math.Ceiling((double)bytes.Length/(rgbImage.Width*3));
byte[] newMakerNoteImage = new byte[rgbImage[0].Data.Length + (rgbImage.Width * 3 * padding)];
Buffer.BlockCopy(rgbImage[0].Data, 0, newMakerNoteImage, 0, rgbImage[0].Data.Length);
Buffer.BlockCopy(bytes, 0, newMakerNoteImage, rgbImage[0].Data.Length, bytes.Length);

BitmapPalette myPalette = BitmapPalettes.WebPalette;

           // Creates a new empty image with the pre-defined palette
           BitmapSource image = BitmapSource.Create(
               rgbImage.Width,
               rgbImage.Height,
               96,
               96,
               PixelFormats.Bgr24,
               myPalette,
               newMakerNoteImage,
               rgbImage.Width * 3);

           FileStream stream = new FileStream(@"C:\TEMP\new.jpg", FileMode.Create);
           JpegBitmapEncoder encoder = new JpegBitmapEncoder();
           encoder.FlipHorizontal = false;
           encoder.FlipVertical = false;
           encoder.QualityLevel = 30;
           encoder.Frames.Add(BitmapFrame.Create(image));
           encoder.Save(stream);

图像输出正常,但二进制数据未添加到图像的末尾。 你能告诉我我是否正确地做到了吗?

我想我可能会看到这一切都错了,我需要使用EXIF才能将这个制造商数据添加到图像中。数据对图像的用户来说是不可见的。

1 个答案:

答案 0 :(得分:0)

这是我为EXIF / MakerNote创建的测试解决方案 据我所知,37500是EXIF中的makernote十六进制标签。

http://nicholasarmstrong.com/2010/02/exif-quick-reference/

 public void CreateMakerNoteJpgImage(byte[] makerNoteArray, string path)
        {
            BitmapPalette myPalette = BitmapPalettes.WebPalette;

            // Creates a new empty image with the pre-defined palette
            BitmapSource image = BitmapSource.Create(
                Width,
                Height,
                96,
                96,
                PixelFormats.Bgr24,
                myPalette,
                _channels[0].Data,
                Width * 3);

            FileStream stream = new FileStream(path, FileMode.Create);

            BitmapMetadata metadata = new BitmapMetadata("jpg");
            //adding makernote data into EXIF of the jpeg image
            metadata.SetQuery("/app1/ifd/exif:{uint=37500}", makerNoteArray);

            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.FlipHorizontal = false;
            encoder.FlipVertical = false;
            encoder.QualityLevel = 30;
            BitmapFrame frame = BitmapFrame.Create(image, null, metadata, null);
            encoder.Frames.Add(frame);
            encoder.Save(stream);
        }