public bool AddCategoryImage(int category_id, HttpPostedFileBase _Image, string category_title = "", string caption = "", string description = "") { VideoCategory category = DAManager.VideoCategoriesRepository.Get(cat => cat.CategoryId == category_id).FirstOrDefault(); if (category == null) { return(false); } if (category.Image != null) { DeleteCategoryimage(category.ImageId.Value); } if (_Image != null) { Image img = new Image() { AddDate = DateTime.Now, Caption = caption, Description = description, URL = "" }; DAManager.ImagesRepository.Insert(img); category.Image = img; try { //Save Image in the data base to get its id. DAManager.Save(); //set the image file name string file_name = ""; if (category_title == "") { file_name = (img.ImageId.ToString() + "-" + _Image.FileName).Replace(" ", "-"); } else { file_name = (img.ImageId.ToString() + "-" + category_title + Path.GetExtension(_Image.FileName)).Replace(" ", "-"); } System.Drawing.Image web_image = System.Drawing.Image.FromStream(_Image.InputStream); //save Original Image ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name); //Save Thumnails ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, CategoryThumbWidth, CategoryThumbHeight); //Update the DB value img.URL = ImageService.GetImagesDirectory() + file_name; DAManager.Save(); } catch (Exception ex) { logService.WriteError(ex.Message, ex.Message, ex.StackTrace, ex.Source); return(false); } } return(true); }
public static System.Drawing.Color GetFirstColor(System.Drawing.Image image) { if (image == null) { return(System.Drawing.Color.White); } System.Drawing.Color clrReturn = System.Drawing.Color.White; System.Drawing.Bitmap bmp; System.Drawing.Imaging.BitmapData bmData; System.IntPtr ptr; int nOffSet, x, y, red, blue, green; bmp = (System.Drawing.Bitmap)image.Clone(); bmData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); ptr = bmData.Scan0; nOffSet = bmp.Height; for (y = 0; y < 1; y++) { for (x = 0; x < 1; x++) { blue = System.Runtime.InteropServices.Marshal.ReadByte(ptr, 0); green = System.Runtime.InteropServices.Marshal.ReadByte(ptr, 1); red = System.Runtime.InteropServices.Marshal.ReadByte(ptr, 2); clrReturn = System.Drawing.Color.FromArgb(red, green, blue); ptr = new System.IntPtr(ptr.ToInt32() + 3); } ptr = bmData.Scan0; ptr = new System.IntPtr(ptr.ToInt32() + (bmData.Stride * y)); } ptr = new System.IntPtr(ptr.ToInt32() + nOffSet); bmp.UnlockBits(bmData); return(clrReturn); }
public static System.Drawing.Color GetFirstColorHits(System.Drawing.Image image) { //TODO: Work over here - Not Ready System.Drawing.Color clrReturn = System.Drawing.Color.White; System.Collections.ArrayList arlColors = new System.Collections.ArrayList(); System.Collections.ArrayList arlColorsHits = new System.Collections.ArrayList(); System.Drawing.Bitmap bmp; System.Drawing.Imaging.BitmapData bmData; System.IntPtr ptr; int nOffSet, x, y, red, blue, green; bmp = (System.Drawing.Bitmap)image.Clone(); bmData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb); ptr = bmData.Scan0; nOffSet = bmp.Height; for (y = 0; y < bmp.Height; y++) { for (x = 0; x < bmData.Width + 1; x++) { blue = System.Runtime.InteropServices.Marshal.ReadByte(ptr, 0); green = System.Runtime.InteropServices.Marshal.ReadByte(ptr, 1); red = System.Runtime.InteropServices.Marshal.ReadByte(ptr, 2); System.Drawing.Color clrCurrent = System.Drawing.Color.FromArgb(red, green, blue); IncreaseColorHits(ref arlColors, ref arlColorsHits, clrCurrent); ptr = new System.IntPtr(ptr.ToInt32() + 3); } ptr = bmData.Scan0; ptr = new System.IntPtr(ptr.ToInt32() + (bmData.Stride * y)); } ptr = new System.IntPtr(ptr.ToInt32() + nOffSet); bmp.UnlockBits(bmData); clrReturn = GetHigherColorHits(ref arlColors, ref arlColorsHits); return(clrReturn); }
public static string ImageToBase64(System.Drawing.Image pi) { try { string base64String = null; //string path = @"D:\UBA\henry.jpg"; //string path = @"C:\dermalog\fingerimage\myfinger.bmp"; //string path = pi.ImageLocation; //using (System.Drawing.Image image = System.Drawing.Image.FromFile(path)) using (System.Drawing.Image image = pi.Clone() as System.Drawing.Image) { using (MemoryStream m = new MemoryStream()) { //pi.Image.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg); image.Save(m, image.RawFormat); //image.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] imageBytes = m.ToArray(); base64String = Convert.ToBase64String(imageBytes); return(base64String); } } } catch (Exception ex) { string str = ex.Message; return(null); } }
private void SetNewVideoImage() { if (droneControl.IsConnected) { System.Drawing.Image image = droneControl.BitmapImage; if (image != null) { frameCountSinceLastCapture++; if (videoRecorder.IsVideoCaptureRunning) { videoRecorder.AddFrame((System.Drawing.Bitmap)image.Clone()); } } ImageSource imageSource = droneControl.ImageSourceImage; if (imageSource != null && (droneControl.CurrentCameraType == DroneCameraMode.FrontCamera || droneControl.CurrentCameraType == DroneCameraMode.PictureInPictureFront)) { ImageSource resultingSource = hudInterface.DrawHud((BitmapSource)imageSource); imageVideo.Source = resultingSource; } else { imageVideo.Source = imageSource; } } }
public void SendLiveImage(System.Drawing.Image img) { lock (this.locker) { if (this.images.Count < 2) { this.images.Enqueue((System.Drawing.Image)img.Clone()); this.go.Set(); } } }
private void InitializeNonUITasks() { try { presentation = Presentation.Open(filePath); } catch (Exception exp) { MessageBox.Show("This PowerPoint Presentation cannot be opened properly, please contact Syncfusion support"); return; } slideImageSources.Clear(); thumbnailImageSource.Clear(); printImages.Clear(); currentSlideNumber = 0; presentation.ChartToImageConverter = new ChartToImageConverter(); presentation.ChartToImageConverter.ScalingMode = Syncfusion.OfficeChart.ScalingMode.Best; try { foreach (ISlide slide in presentation.Slides) { using (System.Drawing.Image image = slide.ConvertToImage(Syncfusion.Drawing.ImageType.Bitmap)) { System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); printImages.Add(image.Clone() as System.Drawing.Image); System.Drawing.Image newImage = image.GetThumbnailImage(170, 100, myCallback, System.IntPtr.Zero); using (Stream ms = new MemoryStream()) { newImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png); var decoder = BitmapDecoder.Create(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); thumbnailImageSource.Add(decoder.Frames[0]); } using (Stream ms = new MemoryStream()) { image.Save(ms, System.Drawing.Imaging.ImageFormat.Png); var decoder = BitmapDecoder.Create(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); slideImageSources.Add(decoder.Frames[0]); } } } } catch (Exception) { MessageBox.Show("This PowerPoint Presentation cannot be converted to images properly, please contact Syncfusion support"); return; } }
public bool AddPollImage(int poll_id, HttpPostedFileBase Poll_Image) { Poll poll = DAManager.PollsRepository.Get(p => p.PollId == poll_id).FirstOrDefault(); if (poll == null) { return(false); } //Delete old Image if (poll.ImageURL != null && poll.ImageURL != "") { ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + poll.ImageURL)); } if (Poll_Image != null) { try { //Save Image in the data base to get its id. DAManager.Save(); //set the image file name string file_name = ""; file_name = ("poll_" + poll_id + "-" + Poll_Image.FileName).Replace(" ", "-"); System.Drawing.Image web_image = System.Drawing.Image.FromStream(Poll_Image.InputStream); //save Original Image ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name); //Update the DB value poll.ImageURL = ImageService.GetImagesDirectory() + file_name; DAManager.Save(); } catch (Exception ex) { logService.WriteError(ex.Message, ex.Message, ex.StackTrace, ex.Source); return(false); } } return(true); }
private void printDoc_PrintPage(object sender, PrintPageEventArgs e) { System.Drawing.Rectangle bounds = System.Drawing.Rectangle.Empty; if (pageBounds) { bounds = e.PageBounds; } else { bounds = e.MarginBounds; } System.Drawing.Image tmp = (System.Drawing.Image)picture.Clone(); if (stretch == Stretch.Fill) { tmp = picture.Resize(bounds.Width, bounds.Height); } else if (stretch == Stretch.Uniform) { tmp = picture.ResizeProportional(bounds.Width, bounds.Height); } int x = bounds.Left, y = bounds.Top; if (alignX == AlignmentX.Center) { x = bounds.Left + bounds.Width / 2 - tmp.Width / 2; } else if (alignX == AlignmentX.Right) { x = bounds.Left + bounds.Width - tmp.Width; } if (alignY == AlignmentY.Center) { y = bounds.Top + bounds.Height / 2 - tmp.Height / 2; } else if (alignY == AlignmentY.Bottom) { y = bounds.Top + bounds.Height - tmp.Height; } e.Graphics.DrawImage(tmp, new System.Drawing.Point(x, y)); }
private void SetNewVideoImage() { if (arDroneControl.IsConnected) { System.Drawing.Image newImage = arDroneControl.GetDisplayedImage(); if (newImage != null) { frameCountSinceLastCapture++; if (videoRecorder.IsVideoCaptureRunning) { videoRecorder.AddFrame((System.Drawing.Bitmap)newImage.Clone()); } BitmapImage newBitmapImage = Utility.CreateBitmapImageFromImage(newImage); imageVideo.Source = newBitmapImage; } } }
public bool AddAuthorImages(int author_id, IEnumerable <HttpPostedFileBase> Images, string english_author_name = "", string caption = "", string description = "") { Author author = DAManager.AuthorsRepository.Get(auth => auth.AuthorId == author_id).FirstOrDefault(); if (author == null) { return(false); } if (Images != null) { foreach (HttpPostedFileBase image in Images) { if (image != null) { Image img = new Image() { AddDate = DateTime.Now, Caption = caption, Description = description, URL = "" }; DAManager.ImagesRepository.Insert(img); author.AuthorImages.Add(new AuthorImage() { Author = author, Image = img }); try { DAManager.Save(); //Original string file_name = ""; if (english_author_name == "") { file_name = (img.ImageId.ToString() + "-" + image.FileName).Replace(" ", "-"); } else { file_name = (img.ImageId.ToString() + "-" + english_author_name + Path.GetExtension(image.FileName)).Replace(" ", "-"); } System.Drawing.Image web_image = System.Drawing.Image.FromStream(image.InputStream); //save Original Image ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name); //Save Thumnails ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, AuthorThumbWidth, AuthorThumbHeight); ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, AuthorThumbWidth1, AuthorThumbHeight1); //Update the DB value img.URL = ImageService.GetImagesDirectory() + file_name; DAManager.Save(); } catch (Exception ex) { return(false); } } } } return(true); }
public bool UpdateArticleImage(int image_id, HttpPostedFileBase Image, string article_title = "", string caption = "", string description = "") { Image old_image = DAManager.ImagesRepository.Get(im => im.ImageId == image_id).FirstOrDefault(); if (old_image == null) { return(false); } old_image.Caption = caption; old_image.Description = description; if (Image != null) { try { string FileName = Path.GetFileName(old_image.URL); string Directory = Path.GetDirectoryName(old_image.URL); ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + old_image.URL)); ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + old_image.URL), ArticleThumbWidth.ToString(), ArticleThumbHeight.ToString()); //ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + old_image.URL), ArticleThumbWidth2.ToString(), ArticleThumbHeight2.ToString()); ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + old_image.URL), ArticleThumbWidth3.ToString(), ArticleThumbHeight3.ToString()); ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + old_image.URL), ArticleThumbWidth4.ToString(), ArticleThumbHeight4.ToString()); //ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + old_image.URL), ArticleThumbWidth5.ToString(), ArticleThumbHeight5.ToString()); ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + old_image.URL), ArticleThumbWidth6.ToString(), ArticleThumbHeight6.ToString()); ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + old_image.URL), ArticleThumbWidth7.ToString(), ArticleThumbHeight7.ToString()); ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + old_image.URL), ArticleThumbWidth8.ToString(), ArticleThumbHeight8.ToString()); //Original string file_name = ""; if (article_title == "") { file_name = (old_image.ImageId.ToString() + "-" + Image.FileName).Replace(" ", "-"); } else { file_name = (old_image.ImageId.ToString() + "-" + article_title + Path.GetExtension(Image.FileName)).Replace(" ", "-"); } System.Drawing.Image web_image = System.Drawing.Image.FromStream(Image.InputStream); //save Original Image ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name); //Save Thumnails 200x200 ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, ArticleThumbWidth, ArticleThumbHeight); //ImageService.SaveImage(web_image.Clone(), file_name, ArticleThumbWidth2, ArticleThumbHeight2); ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, ArticleThumbWidth3, ArticleThumbHeight3); ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, ArticleThumbWidth4, ArticleThumbHeight4); //ImageService.SaveImage(web_image.Clone(), file_name, ArticleThumbWidth5, ArticleThumbHeight5); ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, ArticleThumbWidth6, ArticleThumbHeight6); ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, ArticleThumbWidth7, ArticleThumbHeight7); ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, ArticleThumbWidth8, ArticleThumbHeight8); //Update the DB value old_image.URL = ImageService.GetImagesDirectory() + file_name; } catch (Exception ex) { logService.WriteError(ex.Message, ex.Message, ex.StackTrace, ex.Source); return(false); } } try { DAManager.Save(); } catch (Exception ex) { logService.WriteError(ex.Message, ex.Message, ex.StackTrace, ex.Source); return(false); } return(true); }
public bool AddArticleImages(int article_id, IEnumerable <HttpPostedFileBase> Images, string article_title = "", string caption = "", string description = "") { Article article = DAManager.ArticlesRepository.Get(art => art.ArticleId == article_id).FirstOrDefault(); if (article == null) { return(false); } if (Images != null) { foreach (HttpPostedFileBase image in Images) { if (image != null) { Image img = new Image() { AddDate = DateTime.Now, Caption = caption, Description = description, URL = "" }; DAManager.ImagesRepository.Insert(img); article.ArticleImages.Add(new ArticleImage() { Article = article, Image = img }); try { DAManager.Save(); //Original string file_name = ""; if (article_title == "") { file_name = (img.ImageId.ToString() + "-" + image.FileName).Replace(" ", "-"); } else { file_name = (img.ImageId.ToString() + "-" + article_title + Path.GetExtension(image.FileName)).Replace(" ", "-"); } System.Drawing.Image web_image = System.Drawing.Image.FromStream(image.InputStream); //save Original Image ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name); //Save Thumnails ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, ArticleThumbWidth, ArticleThumbHeight); //ImageService.SaveImage(web_image.Clone(), file_name, ArticleThumbWidth2, ArticleThumbHeight2); ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, ArticleThumbWidth3, ArticleThumbHeight3); ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, ArticleThumbWidth4, ArticleThumbHeight4); //ImageService.SaveImage(web_image.Clone(), file_name, ArticleThumbWidth5, ArticleThumbHeight5); ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, ArticleThumbWidth6, ArticleThumbHeight6); ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, ArticleThumbWidth7, ArticleThumbHeight7); ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, ArticleThumbWidth8, ArticleThumbHeight8); //Update the DB value img.URL = ImageService.GetImagesDirectory() + file_name; DAManager.Save(); } catch (Exception ex) { return(false); } } } } return(true); }
public bool UpdateCategoryImage(int image_id, HttpPostedFileBase Image, string category_title = "", string caption = "", string description = "") { ImageService image_manager = new ImageService(); Image old_image = DAManager.ImagesRepository.Get(im => im.ImageId == image_id).FirstOrDefault(); if (old_image == null) { return(false); } old_image.Caption = caption; old_image.Description = description; if (Image != null) { try { ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + old_image.URL)); ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + old_image.URL), CategoryThumbWidth.ToString(), CategoryThumbHeight.ToString()); //Original string file_name = ""; if (category_title == "") { file_name = (old_image.ImageId.ToString() + "-" + Image.FileName).Replace(" ", "-"); } else { file_name = (old_image.ImageId.ToString() + "-" + category_title + Path.GetExtension(Image.FileName)).Replace(" ", "-"); } System.Drawing.Image web_image = System.Drawing.Image.FromStream(Image.InputStream); //save Original Image ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name); //Save Thumnails 200x200 ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, CategoryThumbWidth, CategoryThumbHeight); //Update the DB value old_image.URL = ImageService.GetImagesDirectory() + file_name; } catch (Exception ex) { logService.WriteError(ex.Message, ex.Message, ex.StackTrace, ex.Source); return(false); } } try { DAManager.Save(); } catch (Exception ex) { logService.WriteError(ex.Message, ex.Message, ex.StackTrace, ex.Source); return(false); } return(true); }
internal void LoadImageDataFromByte(Triggernometry.RealPlugin plug, Graphics g, byte[] data) { GifData gif = GetGifData(data); using (MemoryStream ms = new MemoryStream(data)) { using (System.Drawing.Image i = System.Drawing.Image.FromStream(ms)) { System.Drawing.Bitmap b = (System.Drawing.Bitmap)i; System.Drawing.Imaging.FrameDimension CurrentFd = new System.Drawing.Imaging.FrameDimension(i.FrameDimensionsList[0]); NumberOfFrames = i.GetFrameCount(CurrentFd); IsAnimated = (NumberOfFrames > 1); if (IsAnimated == true) { Frames = new List <Image>(); FrameDelays = new List <int>(); System.Drawing.Imaging.PropertyItem delay = i.GetPropertyItem(0x5100); Color tc; bool hastc = false; if (gif.TransparencyIndex >= 0) { tc = gif.Palette[gif.TransparencyIndex]; hastc = true; } else { tc = gif.Palette[0]; } for (int h = 0; h < NumberOfFrames; h++) { int delayn = (delay.Value[h * 4] + (delay.Value[(h * 4) + 1] * 256)) * 10; FrameDelays.Add(delayn); i.SelectActiveFrame(CurrentFd, h); System.Drawing.Image ifa = ((System.Drawing.Image)i.Clone()); byte[] idata = ImageToByte(ifa); if (hastc == true) { if (gif.TransparencyIndex != gif.BackgroundColor) { // hack in case transparency color is different from bgcolor (some gifs have this shit) SetTransparencyIndex(idata, (byte)gif.BackgroundColor); } else { for (int j = 0; j < gif.TransparencyIndex; j++) { if ((gif.Palette[j].R == tc.R) && (gif.Palette[j].R == tc.G) && (gif.Palette[j].R == tc.B)) { // net itself might have selected an earlier color as new transparency color // hack to reset transparency index to match if so SetTransparencyIndex(idata, (byte)j); break; } } } } Frames.Add(g.CreateImage(idata)); } CurrentFrame = -1; AdvanceFrame(); } else { OriginalImage = g.CreateImage(data); } } } }
/// <summary> /// Method to rotate an Image object. The result can be one of three cases: /// - upsizeOk = true: output image will be larger than the input, and no clipping occurs /// - upsizeOk = false & clipOk = true: output same size as input, clipping occurs /// - upsizeOk = false & clipOk = false: output same size as input, image reduced, no clipping /// /// A background color must be specified, and this color will fill the edges that are not /// occupied by the rotated image. If color = transparent the output image will be 32-bit, /// otherwise the output image will be 24-bit. /// /// Note that this method always returns a new Bitmap object, even if rotation is zero - in /// which case the returned object is a clone of the input object. /// </summary> /// <param name="inputImage">input Image object, is not modified</param> /// <param name="angleDegrees">angle of rotation, in degrees</param> /// <param name="upsizeOk">see comments above</param> /// <param name="clipOk">see comments above, not used if upsizeOk = true</param> /// <param name="backgroundColor">color to fill exposed parts of the background</param> /// <returns>new Bitmap object, may be larger than input image</returns> public static System.Drawing.Bitmap RotateImage(System.Drawing.Image inputImage, float angleDegrees, bool upsizeOk, bool clipOk, System.Drawing.Color backgroundColor) { // Test for zero rotation and return a clone of the input image if (angleDegrees == 0f) { return((System.Drawing.Bitmap)inputImage.Clone()); } // Set up old and new image dimensions, assuming upsizing not wanted and clipping OK int oldWidth = inputImage.Width; int oldHeight = inputImage.Height; int newWidth = oldWidth; int newHeight = oldHeight; float scaleFactor = 1f; // If upsizing wanted or clipping not OK calculate the size of the resulting bitmap if (upsizeOk || !clipOk) { double angleRadians = angleDegrees * Math.PI / 180d; double cos = Math.Abs(Math.Cos(angleRadians)); double sin = Math.Abs(Math.Sin(angleRadians)); newWidth = (int)Math.Round(oldWidth * cos + oldHeight * sin); newHeight = (int)Math.Round(oldWidth * sin + oldHeight * cos); } // If upsizing not wanted and clipping not OK need a scaling factor if (!upsizeOk && !clipOk) { scaleFactor = Math.Min((float)oldWidth / newWidth, (float)oldHeight / newHeight); newWidth = oldWidth; newHeight = oldHeight; } // Create the new bitmap object. If background color is transparent it must be 32-bit, // otherwise 24-bit is good enough. var newBitmap = new System.Drawing.Bitmap(newWidth, newHeight, backgroundColor == System.Drawing.Color.Transparent ? System.Drawing.Imaging.PixelFormat.Format32bppArgb : System.Drawing.Imaging.PixelFormat.Format24bppRgb); newBitmap.SetResolution(inputImage.HorizontalResolution, inputImage.VerticalResolution); // Create the Graphics object that does the work using (System.Drawing.Graphics graphicsObject = System.Drawing.Graphics.FromImage(newBitmap)) { graphicsObject.InterpolationMode = InterpolationMode.HighQualityBicubic; graphicsObject.PixelOffsetMode = PixelOffsetMode.HighQuality; graphicsObject.SmoothingMode = SmoothingMode.HighQuality; // Fill in the specified background color if necessary if (backgroundColor != System.Drawing.Color.Transparent) { graphicsObject.Clear(backgroundColor); } // Set up the built-in transformation matrix to do the rotation and maybe scaling graphicsObject.TranslateTransform(newWidth / 2f, newHeight / 2f); if (scaleFactor != 1f) { graphicsObject.ScaleTransform(scaleFactor, scaleFactor); } graphicsObject.RotateTransform(angleDegrees); graphicsObject.TranslateTransform(-oldWidth / 2f, -oldHeight / 2f); // Draw the result graphicsObject.DrawImage(inputImage, 0, 0); } return(newBitmap); }
public System.Drawing.Image CopyMetadataTo(System.Drawing.Image image) { System.Drawing.Image ret = (System.Drawing.Image)image.Clone(); BitmapMetadata myBitmapMetadata = null; JpegBitmapEncoder encoder3 = new JpegBitmapEncoder(); using (MemoryStream stmSrc = new MemoryStream()) { _image.Save(stmSrc, System.Drawing.Imaging.ImageFormat.Jpeg); stmSrc.Position = 0; BitmapDecoder decoderSrc = BitmapDecoder.Create(stmSrc, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None); // // Check to see that the image's metadata is not null // /*/if (decoderSrc.Frames[0].Metadata == null) * { * myBitmapMetadata = new BitmapMetadata("jpg"); * } * else * { * myBitmapMetadata = decoderSrc.Frames[0].Metadata as BitmapMetadata; * }*/ myBitmapMetadata = new BitmapMetadata("jpg"); myBitmapMetadata.ApplicationName = "Photoe.com.br Digital Image Transfer"; myBitmapMetadata.Author = new ReadOnlyCollection <string>( new List <string>() { "Helvio Junior" }); myBitmapMetadata.CameraManufacturer = _metadata.CameraManufacturer; myBitmapMetadata.CameraModel = _metadata.CameraModel; myBitmapMetadata.Comment = _metadata.Comment; myBitmapMetadata.Copyright = "photoe.com.br - Helvio Junior"; myBitmapMetadata.DateTaken = _metadata.DateTaken; //myBitmapMetadata.Keywords = new ReadOnlyCollection<string>(new List<string>() { "Lori", "Kane" }); myBitmapMetadata.Keywords = _metadata.Keywords; myBitmapMetadata.Rating = _metadata.Rating; myBitmapMetadata.Subject = _metadata.Subject; myBitmapMetadata.Title = _metadata.Title; // Create a new frame that is identical to the one // from the original image, except for the new metadata. encoder3.Frames.Add( BitmapFrame.Create( decoderSrc.Frames[0], decoderSrc.Frames[0].Thumbnail, myBitmapMetadata, decoderSrc.Frames[0].ColorContexts)); FileInfo tmpFile = new FileInfo(Path.GetTempFileName()); using (Stream tempStream = File.Open(tmpFile.FullName, FileMode.Create, FileAccess.ReadWrite)) { encoder3.Save(tempStream); } ret = System.Drawing.Image.FromFile(tmpFile.FullName); try { tmpFile.Delete(); } catch { } /* * using (MemoryStream stm2 = new MemoryStream()) * { * * encoder3.Save(stm2); * stm2.Position = 0; * * ret = System.Drawing.Image.FromStream(stm2); * }*/ } return(ret); }
public bool AddUserImages(int user_id, IEnumerable <HttpPostedFileBase> Images, string user_name = "", string caption = "", string description = "") { UserProfile user = DAManager.UserProfilesRepository.Get(us => us.UserId == user_id).FirstOrDefault(); if (user == null) { return(false); } if (Images != null) { foreach (HttpPostedFileBase image in Images) { if (image != null) { Image img = new Image() { AddDate = DateTime.Now, Caption = caption, Description = description, URL = "" }; DAManager.ImagesRepository.Insert(img); user.UserImages.Add(new UserImage() { UserProfile = user, Image = img }); try { DAManager.Save(); //Original string file_name = ""; if (user_name == "") { file_name = (img.ImageId.ToString() + "-" + image.FileName).Replace(" ", "-"); } else { file_name = (img.ImageId.ToString() + "-" + user_name + Path.GetExtension(image.FileName)).Replace(" ", "-"); } System.Drawing.Image web_image = System.Drawing.Image.FromStream(image.InputStream); //save Original Image ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name); //Save Thumnails 200x200 ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, UserThumbWidth, UserThumbHeight); ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, UserMediumThumbWidth, UserMediumThumbHeight); ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, UserSmallThumbWidth, UserSmallThumbHeight); ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, UserThumbWidth2, UserThumbHeight2); //Update the DB value img.URL = ImageService.GetImagesDirectory() + file_name; user.Avatar = img.URL; DAManager.Save(); } catch (Exception ex) { logService.WriteError(ex.Message, ex.Message, ex.StackTrace, ex.Source); return(false); } } } } return(true); }
public GifInfo(string filePath) { bool isWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows); if (System.IO.File.Exists(filePath)) { using (System.Drawing.Image image = System.Drawing.Image.FromFile(filePath)) { this.size = new System.Drawing.Size(image.Width, image.Height); if (image.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif)) { this.frames = new System.Collections.Generic.List <System.Drawing.Image>(); this.fileInfo = new System.IO.FileInfo(filePath); if (System.Drawing.ImageAnimator.CanAnimate(image)) { // Get frames System.Drawing.Imaging.FrameDimension dimension = new System.Drawing.Imaging.FrameDimension(image.FrameDimensionsList[0]); int frameCount = image.GetFrameCount(dimension); int index = 0; int duration = 0; for (int i = 0; i < frameCount; i++) { image.SelectActiveFrame(dimension, i); System.Drawing.Image frame = image.Clone() as System.Drawing.Image; frames.Add(frame); // https://docs.microsoft.com/en-us/dotnet/api/system.drawing.imaging.propertyitem.id?view=dotnet-plat-ext-3.1 // 0x5100 PropertyTagFrameDelay byte[] propertyArray = image.GetPropertyItem(20736).Value; int delay = 0; if (isWindows) { delay = System.BitConverter.ToInt32(propertyArray, index) * 10; } else { delay = System.BitConverter.ToInt32(propertyArray, 0) * 10; } duration += (delay < 100 ? 100 : delay); index += 4; } // Next i this.animationDuration = System.TimeSpan.FromMilliseconds(duration); this.animated = true; this.loop = System.BitConverter.ToInt16(image.GetPropertyItem(20737).Value, 0) != 1; } else { this.frames.Add(image.Clone() as System.Drawing.Image); } } else { throw new System.FormatException("Not valid GIF image format"); } } // End Using image } // End if (System.IO.File.Exists(filePath)) } // End Constructor
public bool UpdateUserImage(int image_id, HttpPostedFileBase Image, string user_name = "", string caption = "", string description = "") { Image old_image = DAManager.ImagesRepository.Get(im => im.ImageId == image_id, null, "UserImages.UserProfile").FirstOrDefault(); if (old_image == null) { return(false); } old_image.Caption = caption; old_image.Description = description; if (Image != null) { try { string FileName = Path.GetFileName(old_image.URL); string Directory = Path.GetDirectoryName(old_image.URL); ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + old_image.URL)); ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + old_image.URL), UserThumbWidth.ToString(), UserThumbHeight.ToString()); ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + old_image.URL), UserSmallThumbWidth.ToString(), UserSmallThumbHeight.ToString()); ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + old_image.URL), UserMediumThumbWidth.ToString(), UserMediumThumbHeight.ToString()); ImageService.DeleteImage(HttpContext.Current.Server.MapPath("~" + old_image.URL), UserThumbWidth2.ToString(), UserThumbHeight2.ToString()); //Original string file_name = ""; if (user_name == "") { file_name = (old_image.ImageId.ToString() + "-" + Image.FileName).Replace(" ", "-"); } else { file_name = (old_image.ImageId.ToString() + "-" + user_name + Path.GetExtension(Image.FileName)).Replace(" ", "-"); } System.Drawing.Image web_image = System.Drawing.Image.FromStream(Image.InputStream); //save Original Image ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name); //Save Thumnails 200x200 ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, UserThumbWidth, UserThumbHeight); ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, UserThumbWidth2, UserThumbHeight2); ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, UserSmallThumbWidth, UserSmallThumbHeight); ImageService.SaveImage((System.Drawing.Image)web_image.Clone(), file_name, UserMediumThumbWidth, UserMediumThumbHeight); //Update the DB value old_image.URL = ImageService.GetImagesDirectory() + file_name; if (old_image.UserImages.FirstOrDefault() != null) { old_image.UserImages.FirstOrDefault().UserProfile.Avatar = old_image.URL; } } catch (Exception ex) { logService.WriteError(ex.Message, ex.Message, ex.StackTrace, ex.Source); return(false); } } try { DAManager.Save(); } catch (Exception ex) { logService.WriteError(ex.Message, ex.Message, ex.StackTrace, ex.Source); return(false); } return(true); }