private void CorrectPhotoNumber(int i, PhotoContext context) { foreach (var guy in context.People.Where(x => x.Id > i)) { guy.CommonPhotoNumber -= 1; } context.SaveChanges(); }
private void PutImage(byte[] commonPhoto, byte[] newPhoto, PhotoContext context) { if (commonPhoto != null) { MemoryStream stream1 = new MemoryStream(commonPhoto); Bitmap bmp1 = new Bitmap(stream1); MemoryStream stream2 = new MemoryStream(newPhoto); Bitmap bmp2 = new Bitmap(stream2); int outputImageWidth = bmp1.Width + bmp2.Width; int outputImageHeight = bmp1.Height > bmp2.Height ? bmp1.Height : bmp2.Height; Bitmap outputImage = new Bitmap(outputImageWidth, outputImageHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics graphics = Graphics.FromImage(outputImage)) { graphics.DrawImage(bmp1, new Rectangle(new Point(), bmp1.Size), new Rectangle(new Point(), bmp1.Size), GraphicsUnit.Pixel); graphics.DrawImage(bmp2, new Rectangle(new Point(bmp1.Width, 0), bmp2.Size), new Rectangle(new Point(), bmp2.Size), GraphicsUnit.Pixel); } MemoryStream m = new MemoryStream(); var p = context.Photos.First(x => x.Id == 1); outputImage.Save(m, ImageFormat.Jpeg); p.Face.Image = m.ToArray(); context.SaveChanges(); outputImage.Save(@"C:\Users\vinnik\Desktop\Новая папка\img.jpg", ImageFormat.Jpeg); } else { MemoryStream stream2 = new MemoryStream(newPhoto); var p = context.Photos.First(x => x.Id == 1); p.Face.Image = stream2.ToArray(); context.SaveChanges(); } }
private void DeleteImage(byte[] commonPhoto, int ownerId, PhotoContext context) { MemoryStream stream1 = new MemoryStream(commonPhoto); Bitmap bmp1 = new Bitmap(stream1); int outputImageWidth = bmp1.Width - 100; int outputImageHeight = bmp1.Height; if (outputImageWidth == 0) { var photo = context.Photos.First(x => x.Id == 1); photo.Face.Image = null; context.SaveChanges(); return; } Bitmap outputImage = new Bitmap(outputImageWidth, outputImageHeight, PixelFormat.Format32bppArgb); int position = context.People.First(x => x.Id == ownerId).CommonPhotoNumber; using (Graphics graphics = Graphics.FromImage(outputImage)) { graphics.DrawImage(bmp1, new Rectangle(0, 0, 100 * position, outputImageHeight), new Rectangle(0, 0, 100 * position, outputImageHeight), GraphicsUnit.Pixel); graphics.DrawImage(bmp1, new Rectangle(100 * position, 0, outputImageWidth - 100 * position, outputImageHeight), new Rectangle(100 * (position + 1), 0, bmp1.Width - 100 * (position + 1), 100), GraphicsUnit.Pixel); } outputImage.Save(@"C:\Users\vinnik\Desktop\Новая папка\img.jpg", ImageFormat.Jpeg); var p = context.Photos.First(x => x.Id == 1); MemoryStream m = new MemoryStream(); outputImage.Save(m, ImageFormat.Jpeg); p.Face.Image = m.ToArray(); context.SaveChanges(); CorrectPhotoNumber(position, context); }
private void ReplaceImage(byte[] commonPhoto, byte[] newPhoto, int position, PhotoContext context) { MemoryStream stream1 = new MemoryStream(commonPhoto); Bitmap bmp1 = new Bitmap(stream1); if (bmp1.Width / 100 <= position) { PutImage(commonPhoto, newPhoto, context); return; } MemoryStream stream2 = new MemoryStream(newPhoto); Bitmap bmp2 = new Bitmap(stream2); int outputImageWidth = bmp1.Width; int outputImageHeight = bmp1.Height; Bitmap outputImage = new Bitmap(outputImageWidth, outputImageHeight, PixelFormat.Format32bppArgb); using (Graphics graphics = Graphics.FromImage(outputImage)) { graphics.DrawImage(bmp1, 0, 0, new Rectangle(0, 0, 100 * position, 100), GraphicsUnit.Pixel); outputImage.Save(@"C:\Users\vinnik\Desktop\Новая папка\img.jpg", ImageFormat.Jpeg); graphics.DrawImage(bmp2, 100 * position, 0, new Rectangle(0, 0, bmp2.Width, 100), GraphicsUnit.Pixel); outputImage.Save(@"C:\Users\vinnik\Desktop\Новая папка\img.jpg", ImageFormat.Jpeg); graphics.DrawImage(bmp1, 100 * (position + 1), 0, new Rectangle(100 * (position + 1), 0, bmp1.Width - (100 * (position + 1)), 100), GraphicsUnit.Pixel); outputImage.Save(@"C:\Users\vinnik\Desktop\Новая папка\img.jpg", ImageFormat.Jpeg); } MemoryStream m = new MemoryStream(); var p = context.Photos.First(x => x.Id == 1); outputImage.Save(m, ImageFormat.Jpeg); p.Face.Image = m.ToArray(); context.SaveChanges(); outputImage.Save(@"C:\Users\vinnik\Desktop\Новая папка\img.jpg", ImageFormat.Jpeg); }