Пример #1
0
        public static AppendContentBuilder FromString(string str)
        {
            var match = reg.Match(str);

            if (!match.Success)
            {
                return(null);
            }

            var builder = new AppendContentBuilder();

            if (match.Groups.Count == 2)
            {
                var captureCollection = match.Groups[1].Captures;
                for (int i = 0; i < captureCollection.Count; i++)
                {
                    var itemStr = captureCollection[i].Value;
                    var items   = itemStr.Substring(1, itemStr.Length - 2).Split(new char[] { ',' });
                    var content = new AppendContent
                    {
                        X     = int.Parse(items[0]),
                        Y     = int.Parse(items[1]),
                        Color = int.Parse(items[2])
                    };
                    builder.Contents.Add(content);
                }
            }
            return(builder);
        }
Пример #2
0
        public void modify(FileInfo file)
        {
            Bitmap               bitmap        = null;
            AppendContent        appendContent = null;
            AppendContentBuilder builder       = AppendContentBuilder.ParseFile(file) ?? new AppendContentBuilder();

            try
            {
                using (var fs = file.OpenRead())
                {
                    bitmap = new Bitmap(fs);
                }

                Random random = new Random((int)DateTime.Now.Ticks);
                for (int i = 0; i < 2048 * 2048; i++)
                {
                    var x     = random.Next(bitmap.Width);
                    var y     = random.Next(bitmap.Height);
                    var pixel = bitmap.GetPixel(x, y);
                    if (pixel.ToArgb() != Color.Transparent.ToArgb())
                    {
                        if (pixel.A > 125)
                        {
                            bitmap.SetPixel(x, y, Color.FromArgb(pixel.A - random.Next(1, 3), pixel));
                        }
                        else
                        {
                            bitmap.SetPixel(x, y, Color.FromArgb(pixel.A + random.Next(1, 3), pixel));
                        }
                        appendContent = new AppendContent {
                            X = x, Y = y, Color = pixel.ToArgb()
                        };
                        break;
                    }
                }

                bitmap.Save(file.FullName);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (bitmap != null)
                {
                    bitmap.Dispose();
                }
            }
            if (appendContent != null)
            {
                builder.Contents.Add(appendContent);
                AppendContentBuilder.AddContent2File(file.FullName, builder.Contents);
            }

            //var png = new Png(file);
            //png.AddInternationalText("comment", "hello " + DateTime.Now.Ticks);
            //png.SaveFile();
        }
Пример #3
0
        public void recovery(FileInfo file)
        {
            Bitmap bitmap     = null;
            var    filebuffer = File.ReadAllBytes(file.FullName);
            var    builder    = AppendContentBuilder.ParseFile(filebuffer);

            if (builder == null)
            {
                return;
            }

            bool success = false;

            try
            {
                using (var fs = file.OpenRead())
                {
                    bitmap = new Bitmap(fs);
                }

                foreach (var apcnt in builder.Contents)
                {
                    bitmap.SetPixel(apcnt.X, apcnt.Y, Color.FromArgb(apcnt.Color));
                }

                bitmap.Save(file.FullName);
                success = true;
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (bitmap != null)
                {
                    bitmap.Dispose();
                }
            }

            if (success)
            {
                AppendContentBuilder.RemoveContentfromFile(file.FullName);
            }
        }
Пример #4
0
        public static void AddContent2File(String file, ISet <AppendContent> contents)
        {
            var fileBuffer = File.ReadAllBytes(file);

            var builder = ParseFile(fileBuffer);

            if (builder == null)
            {
                builder = new AppendContentBuilder();
                foreach (var content in contents)
                {
                    builder.Contents.Add(content);
                }

                using (var fs = new FileStream(file, FileMode.Append, FileAccess.Write))
                {
                    fs.Write(Encoding.ASCII.GetBytes(builder.ToString()), 0, builder.Length);
                }
            }
            else
            {
                int oldlen = builder.Length;
                foreach (var content in contents)
                {
                    builder.Contents.Add(content);
                }
                var contentBuffer = Encoding.ASCII.GetBytes(builder.ToString());
                var resultbuffer  = new byte[fileBuffer.Length + builder.Length];
                Array.Copy(fileBuffer, 0, resultbuffer, 0, fileBuffer.Length - oldlen);
                Array.Copy(contentBuffer, 0, resultbuffer, fileBuffer.Length, contentBuffer.Length);
                using (var fs = File.Create(file))
                {
                    fs.Write(resultbuffer, 0, resultbuffer.Length);
                }
            }
        }