Exemplo n.º 1
0
        public void Save(string text)
        {
            if (this.notEmpty)
            {
                bool         hasOld       = false;
                CommentBlock commentBlock = null;
                int          length       = 0;
                int          start        = 0;
                int          tLen         = 0;
                FileStream   stream       = null;
                FileStream   newStream    = null;
                BinaryReader reader       = null;
                try
                {
                    commentBlock = this.ReadComment();
                    if ((commentBlock != null) && commentBlock.Valid())
                    {
                        hasOld = true;
                        start  = commentBlock.start;
                    }
                    else
                    {
                        start = this.jpegStart();
                    }
                    byte[] commentBytes = Encoding.UTF8.GetBytes(text);
                    length = commentBytes.Length;
                    if (length > 0xffff)
                    {
                        return;
                    }
                    stream = new FileStream(this.filename, FileMode.Open, FileAccess.Read);
                    tLen   = (int)stream.Length;
                    reader = new BinaryReader(stream);
                    byte[] temp   = reader.ReadBytes(start);
                    int    oldLen = 0;
                    if (hasOld)
                    {
                        oldLen = commentBlock.end - commentBlock.start;
                        reader.ReadBytes(oldLen);
                    }
                    byte[] imageData = reader.ReadBytes((tLen - start) - oldLen);
                    stream.Close();

                    newStream = new FileStream(this.filename, FileMode.Create, FileAccess.Write);
                    newStream.Write(temp, 0, start);
                    if (length > 0)
                    {
                        newStream.WriteByte(Convert.ToByte('\x00ff'));
                        newStream.WriteByte(Convert.ToByte('\x00fe'));
                        if (length < 0xff)
                        {
                            newStream.WriteByte(Convert.ToByte('\0'));
                            newStream.WriteByte(Convert.ToByte((int)(length + 2)));
                        }
                        else if (length <= 0xffff)
                        {
                            newStream.WriteByte(Convert.ToByte((length + 2) / 0x100));
                            newStream.WriteByte(Convert.ToByte((length + 2) - ((length + 2) / 0x100 * 0x100)));
                        }
                        newStream.Write(commentBytes, 0, length);
                    }
                    newStream.Write(imageData, 0, (tLen - start) - oldLen);
                }
                finally
                {
                    try
                    {
                        if (reader != null)
                        {
                            reader.Close();
                        }
                        if (stream != null)
                        {
                            stream.Close();
                        }
                        if (newStream != null)
                        {
                            newStream.Close();
                        }
                    }
                    catch
                    {
                    }
                }
            }
        }