public static void WriteASCIIValue(Vt.Jpeg.Jpeg VtJpeg, string TextValue, PropertyTagId Id) { // load the image to change Image PicImage = new Bitmap(VtJpeg.SourceBitmap); System.Drawing.Imaging.Encoder Enc = System.Drawing.Imaging.Encoder.Transformation; EncoderParameters EncParms = new EncoderParameters(1); ImageCodecInfo CodecInfo = Vt.Jpeg.Jpeg.GetEncoderInfo("image/jpeg"); // put the new description into the right property item PropertyItem[] PropertyItems = PicImage.PropertyItems; PropertyItems[0].Id = (int)Id; // 0x010e; PropertyItems[0].Type = (short)PropertyTagType.ASCII; PropertyItems[0].Len = TextValue.Length; PropertyItems[0].Value = VtStrings.StringToByteArray(TextValue); PicImage.SetPropertyItem(PropertyItems[0]); // for lossless rewriting must rotate the image by 90 degrees! EncoderParameter EncParm = new EncoderParameter(Enc, (long)EncoderValue.TransformRotate90); EncParms.Param[0] = EncParm; // ulozim transformovany jpeg do streamu MemoryStream TmpStream = new MemoryStream(); PicImage.Save(TmpStream, CodecInfo, EncParms); // vymaz z pameti PicImage.Dispose(); PicImage = null; // ted ho otocim zpet a dam do puvodniho souboru PicImage = Image.FromStream(TmpStream); EncParm = new EncoderParameter(Enc, (long)EncoderValue.TransformRotate270); EncParms.Param[0] = EncParm; // PicImage.Save(Filename, CodecInfo, EncParms); VtJpeg.LoadFromBitmap(PicImage as Bitmap); PicImage.Dispose(); PicImage = null; }
/// <summary> /// zapise do jpegu ascii textovou exif informaci /// </summary> /// <param name="Filename">jmeno jpeg souboru</param> /// <param name="TextValue">text</param> /// <param name="Id">id cislo exif vlastnosti</param> public static void SaveAndAddImageDateTime(string SourceFile, string DestinationFile, DateTime ImageDateTime) { // prevod datetime na str string StrDateTime = ImageDateTime.ToString("yyyy:MM:dd HH:mm:ss"); // load the image to change Image PicImage = Image.FromFile(SourceFile); System.Drawing.Imaging.Encoder Enc = System.Drawing.Imaging.Encoder.Transformation; EncoderParameters EncParms = new EncoderParameters(1); ImageCodecInfo CodecInfo = Vt.Jpeg.Jpeg.GetEncoderInfo("image/jpeg"); // put the new description into the right property item PropertyItem[] PropertyItems = PicImage.PropertyItems; PropertyItems[0].Id = (int)PropertyTagId.DateTime; PropertyItems[0].Type = (short)PropertyTagType.ASCII; PropertyItems[0].Len = StrDateTime.Length; PropertyItems[0].Value = VtStrings.StrToByteArrayCS(StrDateTime); PicImage.SetPropertyItem(PropertyItems[0]); // for lossless rewriting must rotate the image by 90 degrees! EncoderParameter EncParm = new EncoderParameter(Enc, (long)EncoderValue.TransformRotate90); EncParms.Param[0] = EncParm; // ulozim transformovany jpeg do streamu MemoryStream TmpStream = new MemoryStream(); PicImage.Save(TmpStream, CodecInfo, EncParms); // vymaz z pameti PicImage.Dispose(); PicImage = null; // ted ho otocim zpet a dam do puvodniho souboru PicImage = Image.FromStream(TmpStream); EncParm = new EncoderParameter(Enc, (long)EncoderValue.TransformRotate270); EncParms.Param[0] = EncParm; PicImage.Save(DestinationFile, CodecInfo, EncParms); PicImage.Dispose(); PicImage = null; }
private void GetFileName(string cfgFileName) { if (!string.IsNullOrEmpty(cfgFileName)) { // budu hledat v zaslanym souboru _cfgFileName = cfgFileName; } else { // jinak jmeno cfg souboru dle exe - jen odriznu .vshost.exe string exeFileName = AppDomain.CurrentDomain.FriendlyName; if (exeFileName.EndsWith(".vshost.exe")) { exeFileName = exeFileName.Substring(0, exeFileName.Length - ".vshost.exe".Length); } if (exeFileName.EndsWith(".exe")) { exeFileName = exeFileName.Substring(0, exeFileName.Length - ".exe".Length); } _cfgFileName = VtStrings.AddSlash(AppDomain.CurrentDomain.BaseDirectory) + exeFileName + DEFAULT_EXT; } }
/// <summary> /// samotny prevod PropertyItem.Value na string dle PropertyItem.Type /// </summary> /// <param name="PItem"></param> /// <returns></returns> static private string PropertyValueToString(PropertyItem PItem) { const int BYTEJUMP_SHORT = 2; const int BYTEJUMP_LONG = 4; const int BYTEJUMP_SLONG = 4; const int BYTEJUMP_RATIONAL = 8; const int BYTEJUMP_SRATIONAL = 8; string Result = null; switch ((PropertyTagType)PItem.Type) { case (PropertyTagType.Byte): // 1 { Result = BitConverter.ToString(PItem.Value, 0, PItem.Len); break; } case (PropertyTagType.ASCII): // 2 { /* - puvodni, ale pak nejde cestina !!! * System.Text.ASCIIEncoding Encoding = new System.Text.ASCIIEncoding(); * Result = Encoding.GetString(PItem.Value, 0, PItem.Len - 1); */ // spravny prevod do defaultniho kodovani, tj v ceskych win bude fungovat cestina Result = VtStrings.BytesToStrCS(PItem.Value); break; } case (PropertyTagType.Short): // 3 { for (int i = 0; i < PItem.Len; i = i + BYTEJUMP_SHORT) { System.UInt16 Value = BitConverter.ToUInt16(PItem.Value, i); Result += Value.ToString(); } break; } case (PropertyTagType.Long): // 4 { for (int i = 0; i < PItem.Len; i = i + BYTEJUMP_LONG) { System.UInt32 Value = BitConverter.ToUInt32(PItem.Value, i); Result += Value.ToString(); } break; } case (PropertyTagType.Rational): // 5 { for (int i = 0; i < PItem.Len; i = i + BYTEJUMP_RATIONAL) { System.UInt32 Numer = BitConverter.ToUInt32(PItem.Value, i); System.UInt32 Denom = BitConverter.ToUInt32(PItem.Value, i + BYTEJUMP_LONG); if (Result != null) { Result += ' '; } Result += Numer.ToString() + '/' + Denom.ToString(); } break; } case (PropertyTagType.Undefined): // 7 { Result = BitConverter.ToString(PItem.Value, 0, PItem.Len); break; } case (PropertyTagType.SLONG): // 9 { for (int i = 0; i < PItem.Len; i = i + BYTEJUMP_SLONG) { System.Int32 Value = BitConverter.ToInt32(PItem.Value, i); Result += Value.ToString(); } break; } case (PropertyTagType.SRational): // 10 { for (int i = 0; i < PItem.Len; i = i + BYTEJUMP_SRATIONAL) { System.Int32 Numer = BitConverter.ToInt32(PItem.Value, i); System.Int32 Denom = BitConverter.ToInt32(PItem.Value, i + BYTEJUMP_SLONG); if (Result != null) { Result += ' '; } Result += Numer.ToString() + '/' + Denom.ToString(); } break; } default: break; } return(Result); }