private void FixNoPhotoImage() { string path = @"c:\Temp\NoPhoto300.png"; string key = "NoPhoto"; DateTime now = DateTime.Now; using (Stream stream = File.OpenRead(path)) { Image image = Image.FromStream(stream); var newBlob300 = ImageManager.GetResizedImageBlob(image, 300, 0); File.WriteAllBytes(@"c:\VoteImages\Profile300\" + key + ".png", newBlob300); PoliticiansImagesBlobs.UpdateProfile300(newBlob300, key); PoliticiansImagesBlobs.UpdateProfileOriginal(newBlob300, key); var newBlob200 = ImageManager.GetResizedImageBlob(image, 200, 0); File.WriteAllBytes(@"c:\VoteImages\Profile200\" + key + ".png", newBlob200); PoliticiansImagesBlobs.UpdateProfile200(newBlob200, key); var newBlob100 = ImageManager.GetResizedImageBlob(image, 100, 0); File.WriteAllBytes(@"c:\VoteImages\Headshot100\" + key + ".png", newBlob100); PoliticiansImagesBlobs.UpdateHeadshot100(newBlob100, key); var newBlob75 = ImageManager.GetResizedImageBlob(image, 75, 0); File.WriteAllBytes(@"c:\VoteImages\Headshot75\" + key + ".png", newBlob75); PoliticiansImagesBlobs.UpdateHeadshot75(newBlob75, key); var newBlob50 = ImageManager.GetResizedImageBlob(image, 50, 0); File.WriteAllBytes(@"c:\VoteImages\Headshot50\" + key + ".png", newBlob50); PoliticiansImagesBlobs.UpdateHeadshot50(newBlob50, key); var newBlob35 = ImageManager.GetResizedImageBlob(image, 35, 0); File.WriteAllBytes(@"c:\VoteImages\Headshot35\" + key + ".png", newBlob35); PoliticiansImagesBlobs.UpdateHeadshot35(newBlob35, key); var newBlob25 = ImageManager.GetResizedImageBlob(image, 25, 0); File.WriteAllBytes(@"c:\VoteImages\Headshot25\" + key + ".png", newBlob25); PoliticiansImagesBlobs.UpdateHeadshot25(newBlob25, key); var newBlob20 = ImageManager.GetResizedImageBlob(image, 20, 0); File.WriteAllBytes(@"c:\VoteImages\Headshot20\" + key + ".png", newBlob20); PoliticiansImagesBlobs.UpdateHeadshot20(newBlob20, key); var newBlob15 = ImageManager.GetResizedImageBlob(image, 15, 0); File.WriteAllBytes(@"c:\VoteImages\Headshot15\" + key + ".png", newBlob15); PoliticiansImagesBlobs.UpdateHeadshot15(newBlob15, key); PoliticiansImagesData.UpdateHeadshotDate(now, key); PoliticiansImagesData.UpdateHeadshotResizeDate(now, key); PoliticiansImagesData.UpdateProfileOriginalDate(now, key); AppendStatusText("Complete"); } }
protected void ButtonAsIs_Click(object sender, EventArgs e) { try { var politicianKey = LabelPoliticianKey.Text.Trim(); PoliticiansImagesData.GuaranteePoliticianKeyExists(politicianKey); PoliticiansImagesBlobs.GuaranteePoliticianKeyExists(politicianKey); PoliticiansImagesData.UpdateHeadshotDate(DateTime.UtcNow, politicianKey); CommonCacheInvalidation.ScheduleInvalidation("politicianimage", politicianKey); MarkProcessed(); UpdateRowCountAfterChange(); } catch (Exception ex) { HandleException(ex); } }
private static byte[] UpdatePoliticianImages(string politicianKey, Stream imageStream, DateTime uploadTime, bool includeAllProfileImages, bool includeHeadshotOriginal, bool includeHeadshotResizedImages, bool checkForDuplicates, out Size originalSize, out byte[] originalBlob) { var memoryStream = new MemoryStream(); imageStream.Position = 0; imageStream.CopyTo(memoryStream); imageStream.Position = 0; var image = Image.FromStream(imageStream); originalSize = image.Size; var blob = memoryStream.ToArray(); if (!image.RawFormat.Equals(ImageFormat.Jpeg)) // could be transparent { // draw on a white background var b = new Bitmap(image.Width, image.Height); b.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (var g = Graphics.FromImage(b)) { g.Clear(Color.White); g.DrawImageUnscaled(image, 0, 0); //b.Save(@"c:\temp\zz.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } image = b; } // This is so all subsequent db operations can be updates PoliticiansImagesData.GuaranteePoliticianKeyExists(politicianKey); PoliticiansImagesBlobs.GuaranteePoliticianKeyExists(politicianKey); originalBlob = null; if (checkForDuplicates) { // We fetch this image to see if its a duplicate var duplicateTester = GetImageToUseForDuplicateTesting(); if (duplicateTester != null) { var duplicateTesterBlob = GetResizedImageBlobAsJpg(image, duplicateTester.MaxWidth, duplicateTester.MaxHeight); originalBlob = duplicateTesterBlob; var dbBlob = PoliticiansImagesBlobs.GetColumn(duplicateTester.BlobsColumn, politicianKey) as Byte[]; if (dbBlob != null && dbBlob.Length == duplicateTesterBlob.Length) { var isDuplicate = !dbBlob.Where((t, inx) => t != duplicateTesterBlob[inx]) .Any(); if (isDuplicate) { return(null); // signals duplicate } } } } if (includeAllProfileImages) { PoliticiansImagesData.UpdateProfileOriginalDate(uploadTime, politicianKey); //PoliticiansImagesBlobs.UpdateProfileOriginal(blob, politicianKey); foreach (var info in GetAllProfilePoliticianImageInfos()) { var resizedBlob = GetResizedImageBlobAsJpg(image, info.MaxWidth, info.MaxHeight); PoliticiansImagesBlobs.UpdateColumn(info.BlobsColumn, resizedBlob, politicianKey); } } if (includeHeadshotOriginal) { PoliticiansImagesData.UpdateHeadshotDate(uploadTime, politicianKey); } if (includeHeadshotResizedImages) { foreach (var info in GetResizedHeadshotPoliticianImageInfos()) { var resizedBlob = GetResizedImageBlobAsJpg(image, info.MaxWidth, info.MaxHeight); PoliticiansImagesBlobs.UpdateColumn(info.BlobsColumn, resizedBlob, politicianKey); } PoliticiansImagesData.UpdateHeadshotResizeDate(uploadTime, politicianKey); } // Don't do this here. It belongs as a separate call. //CommonCacheInvalidation.ScheduleInvalidation("politicianimage", politicianKey); // We return the blob for logging purposes return(blob); }