public static Image EnsureMaximumDimensions(Image image, int maxWidth, int maxHeight) { // Prevent using images internal thumbnail image.RotateFlip(RotateFlipType.Rotate180FlipNone); image.RotateFlip(RotateFlipType.Rotate180FlipNone); var aspectRatio = ((double)image.Width) / image.Height; //AR = L/A; int imageWidth = image.Width; int imageHeight = image.Height; if (imageWidth > maxWidth) { imageWidth = maxWidth; imageHeight = (int)(imageWidth / aspectRatio); } if (imageHeight > maxHeight) { imageHeight = maxHeight; imageWidth = (int)(imageHeight * aspectRatio); } if (image.Width != imageWidth || image.Height != imageHeight) return image.GetThumbnailImage(imageWidth, imageHeight, null, IntPtr.Zero); return image; }
private void Normalize() { bool rotated = false; try { var property = image.GetPropertyItem(OrientationPropertyId); var rotateFlip = GetCurrentImageOrientation(GetPropertyValue(property)).GetNormalizationRotation(); property.Value = BitConverter.GetBytes((short)1); image.SetPropertyItem(property); if (rotateFlip != RotateFlipType.RotateNoneFlipNone) { rotated = true; image.RotateFlip(rotateFlip); } } catch (Exception) { // Possible image does not have EXIF properties. May simple rotation } if (image.Width > image.Height) { image.RotateFlip(rotated ? RotateFlipType.Rotate270FlipNone : RotateFlipType.Rotate90FlipNone); } }
public static void SaveImage(System.Drawing.Image FullsizeImage, string serverPath, bool forceImageFormat) { FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); //System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero); Size newSize = new Size(FullsizeImage.Width, FullsizeImage.Height); byte[] buffer; using (Bitmap newImage = new Bitmap(FullsizeImage.Width, FullsizeImage.Height)) { using (Graphics canvas = Graphics.FromImage(newImage)) { canvas.InterpolationMode = InterpolationMode.HighQualityBicubic; canvas.DrawImage(FullsizeImage, new Rectangle(new Point(0, 0), newSize)); MemoryStream m = new MemoryStream(); var extension = Path.GetExtension(serverPath); ImageFormat imageFormat; if (forceImageFormat) { imageFormat = ImageFormat.Png; } else { imageFormat = GetImageFormatFromExtension(extension); } newImage.Save(m, imageFormat); buffer = m.GetBuffer(); } } SaveBytesToFile(buffer, serverPath); }
/// <summary> /// Rotates the image when posted from mobile (mobile pictures were showing up sideways on computers) /// </summary> /// <param name="image">The image model containing the image file from HttpPostedFiledBase class</param> /// <returns>The properly rotated image as a System.Drawing.Image</returns> public System.Drawing.Image ImageRotation(Models.Image image) { System.Drawing.Image originalImage = System.Drawing.Image.FromStream(image.ImageFile.InputStream, true, true); if (originalImage.PropertyIdList.Contains(0x0112)) { int rotationValue = originalImage.GetPropertyItem(0x0112).Value[0]; switch (rotationValue) { case 1: // landscape, do nothing break; case 8: // rotated 90 right // de-rotate: originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate270FlipNone); break; case 3: // bottoms up originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate180FlipNone); break; case 6: // rotated 90 left originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate90FlipNone); break; } } return(originalImage); }
//Image resizing public static System.Drawing.Image ResizeImage(int maxWidth, int maxHeight, System.Drawing.Image Image) { int width = Image.Width; int height = Image.Height; if (width > maxWidth || height > maxHeight) { //The flips are in here to prevent any embedded image thumbnails -- usually from cameras //from displaying as the thumbnail image later, in other words, we want a clean //resize, not a grainy one. Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX); Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX); float ratio = 0; if (width > height) { ratio = (float)width / (float)height; width = maxWidth; height = Convert.ToInt32(Math.Round((float)width / ratio)); } else { ratio = (float)height / (float)width; height = maxHeight; width = Convert.ToInt32(Math.Round((float)height / ratio)); } //return the resized image return(Image.GetThumbnailImage(width, height, null, IntPtr.Zero)); } //return the original resized image return(Image); }
public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider) { System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile); // Prevent using images internal thumbnail FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); if (OnlyResizeIfWider) { if (FullsizeImage.Width <= NewWidth) { NewWidth = FullsizeImage.Width; } } int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width; if (NewHeight > MaxHeight) { // Resize with height instead NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height; NewHeight = MaxHeight; } System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero); // Clear handle to original file so that we can overwrite it if necessary FullsizeImage.Dispose(); // Save resized picture NewImage.Save(NewFile); }
// methode used to crope image as Image //public static Image CropImageFile(Image imgPhoto, int targetW, int targetH, int targetX, int targetY) //{ // Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb); // bmPhoto.SetResolution(72, 72); // Graphics grPhoto = Graphics.FromImage(bmPhoto); // grPhoto.SmoothingMode = SmoothingMode.AntiAlias; // grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; // grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality; // grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), targetX, targetY, targetW, targetH, GraphicsUnit.Pixel); // // Save out to memory and then to a file. We dispose of all objects to make sure the files don't stay locked. // MemoryStream NewImageStream = new MemoryStream(); // bmPhoto.Save(NewImageStream, System.Drawing.Imaging.ImageFormat.Jpeg); // //Image ReturnImage = Image.FromStream(new MemoryStream(NewImageStream)); // // bmPhoto.sava(imgPhoto, System.Drawing.Imaging.ImageFormat.Jpeg); // imgPhoto.Dispose(); // bmPhoto.Dispose(); // grPhoto.Dispose(); // //returning the Croped image as byte stream // //return ReturnImage; //} #endregion #region "GetThumbNail" //byte stream public static byte[] GetThumbNail(byte[] ImageStream, int NewWidth, int MaxHeight, bool OnlyResizeIfWider) { System.Drawing.Image FullsizeImage = System.Drawing.Image.FromStream(new MemoryStream(ImageStream)); // Prevent using images internal thumbnail FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); if (OnlyResizeIfWider) { if (FullsizeImage.Width <= NewWidth) { NewWidth = FullsizeImage.Width; } } int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width; if (NewHeight > MaxHeight) { // Resize with height instead NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height; NewHeight = MaxHeight; } System.Drawing.Image ThumbNail = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero); MemoryStream NewImageStream = new MemoryStream(); ThumbNail.Save(NewImageStream, System.Drawing.Imaging.ImageFormat.Jpeg); // Clear handle to original file so that we can overwrite it if necessary FullsizeImage.Dispose(); ThumbNail.Dispose(); return(NewImageStream.GetBuffer()); }
public static Image RotateThumbImage(Image img) { int ix = 20521; foreach (PropertyItem pi in img.PropertyItems.Where(x => x.Id == ix)) { string s = pi.Value.Length > 0 ? pi.Value[0].ToString() : ""; //if (s.Equals("1")) //img.RotateFlip(RotateFlipType.RotateNoneFlipX); if (s.Equals("2")) img.RotateFlip(RotateFlipType.RotateNoneFlipX); if (s.Equals("3")) img.RotateFlip(RotateFlipType.Rotate180FlipNone); if (s.Equals("4")) img.RotateFlip(RotateFlipType.RotateNoneFlipY); if (s.Equals("5")) img.RotateFlip(RotateFlipType.Rotate90FlipX); if (s.Equals("6")) img.RotateFlip(RotateFlipType.Rotate90FlipNone); if (s.Equals("7")) img.RotateFlip(RotateFlipType.Rotate90FlipY); if (s.Equals("8")) img.RotateFlip(RotateFlipType.Rotate270FlipNone); } return img; }
//Image stream public static System.Drawing.Image GetThumbNail(System.Drawing.Image FullsizeImage, int NewWidth, int MaxHeight, bool OnlyResizeIfWider) { // Prevent using images internal thumbnail FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); if (OnlyResizeIfWider) { if (FullsizeImage.Width <= NewWidth) { NewWidth = FullsizeImage.Width; } } int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width; if (NewHeight > MaxHeight) { // Resize with height instead NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height; NewHeight = MaxHeight; } System.Drawing.Image ThumbNail = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero); // Clear handle to original file so that we can overwrite it if necessary FullsizeImage.Dispose(); return(ThumbNail); }
private void EnsureExifImageRotation(DrImage image, Stream imageContent) { imageContent.Position = 0; using (var imageReader = new ImagePropertyReader(imageContent)) { ImagePropertyReader.Orientation?orientation = imageReader.FindOrientation(); if (orientation != null) { switch (orientation.Value) { case ImagePropertyReader.Orientation.D270: image.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case ImagePropertyReader.Orientation.D180: image.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case ImagePropertyReader.Orientation.D90: image.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } } } }
//Image resizing public System.Drawing.Image ResizeImg(int maxWidth, int maxHeight, System.Drawing.Image Image) { int width = Image.Width; int height = Image.Height; if (width > maxWidth || height > maxHeight) { Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX); Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX); float ratio = 0; if (width > height) { ratio = (float)width / (float)height; width = maxWidth; height = Convert.ToInt32(Math.Round((float)width / ratio)); } else { ratio = (float)height / (float)width; height = maxHeight; width = Convert.ToInt32(Math.Round((float)height / ratio)); } //return the resized image return(Image.GetThumbnailImage(width, height, null, IntPtr.Zero)); } //return the original resized image return(Image); }
//auto rotate and save image private void SaveImage(FileUpload fileUpload) { byte[] imageData = new byte[fileUpload.PostedFile.ContentLength]; fileUpload.PostedFile.InputStream.Read(imageData, 0, fileUpload.PostedFile.ContentLength); MemoryStream ms = new MemoryStream(imageData); System.Drawing.Image originalImage = System.Drawing.Image.FromStream(ms); if (originalImage.PropertyIdList.Contains(0x0112)) { int rotationValue = originalImage.GetPropertyItem(0x0112).Value[0]; switch (rotationValue) { case 1: // landscape, do nothing break; case 8: // rotated 90 right // de-rotate: originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate270FlipNone); break; case 3: // bottoms up originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate180FlipNone); break; case 6: // rotated 90 left originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate90FlipNone); break; } } originalImage.Save(Server.MapPath("~/Data/") + fileUpload.FileName); }
public void ResizeImage(string origFileLocation, string newFileLocation, string origFileName, string newFileName, int newWidth, int maxHeight, bool resizeIfWider) { System.Drawing.Image FullSizeImage = System.Drawing.Image.FromFile(origFileLocation + origFileName); // Ensure the generated thumbnail is not being used by rotating it 360 degrees FullSizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); FullSizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); if (resizeIfWider) { if (FullSizeImage.Width <= newWidth) { //newWidth = FullSizeImage.Width; } } int newHeight = FullSizeImage.Height * newWidth / FullSizeImage.Width; if (newHeight > maxHeight) // Height resize if necessary { //newWidth = FullSizeImage.Width * maxHeight / FullSizeImage.Height; newHeight = maxHeight; } newHeight = maxHeight; // Create the new image with the sizes we've calculated System.Drawing.Image NewImage = FullSizeImage.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero); FullSizeImage.Dispose(); NewImage.Save(newFileLocation + newFileName); }
public static void FitImageToForm(string originalFile, string newFileAddress, int frameWidth, int frameHeight) { System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(originalFile); // Prevent using images internal thumbnail FullsizeImage.RotateFlip(RotateFlipType.Rotate180FlipNone); FullsizeImage.RotateFlip(RotateFlipType.Rotate180FlipNone); int newWidth, newHeight; if (FullsizeImage.Height < FullsizeImage.Width) { newHeight = frameHeight; newWidth = FullsizeImage.Width * Convert.ToInt32((float)newHeight / (float)FullsizeImage.Height); } else { newWidth = frameWidth; newHeight = FullsizeImage.Height * Convert.ToInt32((float)newWidth / (float)FullsizeImage.Width); } System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero); // Clear handle to original file so that we can overwrite it if necessary FullsizeImage.Dispose(); // Save resized picture NewImage.Save(newFileAddress); }
/// <summary> /// Make sure the image is orientated correctly /// </summary> /// <param name="image"></param> public static void Orientate(Image image) { /*if (!conf.ProcessEXIFOrientation) { return; }*/ try { // Get the index of the orientation property. int orientationIndex = Array.IndexOf(image.PropertyIdList, EXIF_ORIENTATION_ID); // If there is no such property, return Unknown. if (orientationIndex < 0) { return; } PropertyItem item = image.GetPropertyItem(EXIF_ORIENTATION_ID); ExifOrientations orientation = (ExifOrientations)item.Value[0]; // Orient the image. switch (orientation) { case ExifOrientations.Unknown: case ExifOrientations.TopLeft: break; case ExifOrientations.TopRight: image.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case ExifOrientations.BottomRight: image.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case ExifOrientations.BottomLeft: image.RotateFlip(RotateFlipType.RotateNoneFlipY); break; case ExifOrientations.LeftTop: image.RotateFlip(RotateFlipType.Rotate90FlipX); break; case ExifOrientations.RightTop: image.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case ExifOrientations.RightBottom: image.RotateFlip(RotateFlipType.Rotate90FlipY); break; case ExifOrientations.LeftBottom: image.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } // Set the orientation to be normal, as we rotated the image. item.Value[0] = (byte)ExifOrientations.TopLeft; image.SetPropertyItem(item); } catch (Exception orientEx) { LOG.Warn("Problem orientating the image: ", orientEx); } }
private System.Drawing.Image rotate(System.Drawing.Image img) { switch (currentRotation) { case 1: img.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case 2: img.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case 3: img.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } return(img); }
public static System.Drawing.Image ResizeImage(string path, string image, decimal width, decimal height) { // check the image exists before attempting to resize it if (!File.Exists(path + image)) { return(null); } // refactor by passing in the path and required width/height to an Image method System.Drawing.Image fullSizeImage = System.Drawing.Image.FromFile(path + image); decimal ratio; // adjust the height/width according to the orientation if (IsPortrait(fullSizeImage.Width, fullSizeImage.Height)) { ratio = Convert.ToDecimal(fullSizeImage.Width) / Convert.ToDecimal(fullSizeImage.Height); // portrait: image height to stay as stated height, width to be adjusted //height = 600; width = height * ratio; } else { ratio = Convert.ToDecimal(fullSizeImage.Height) / Convert.ToDecimal(fullSizeImage.Width); // landscape: image width to stay as stated width, height to be adjusted //width = 800; height = width * ratio; } // check we're not trying to enlarge the image if (fullSizeImage.Width <= width || fullSizeImage.Height <= height) { width = fullSizeImage.Width; height = fullSizeImage.Height; } System.Drawing.Image.GetThumbnailImageAbort callback = new System.Drawing.Image.GetThumbnailImageAbort(ImageCallback); // fix to prevent .NET using the embedded thumbnail fullSizeImage.RotateFlip(RotateFlipType.Rotate180FlipNone); fullSizeImage.RotateFlip(RotateFlipType.Rotate180FlipNone); // create the image System.Drawing.Image largeImage = fullSizeImage.GetThumbnailImage((int)width, (int)height, callback, IntPtr.Zero); // return the image - so that it can either be rendered on the fly, or saved to disk return(largeImage); }
protected override IGatewayHandler ProcessGatewayRequest(HttpContext objContext, string strAction) { IGatewayHandler objGH = null; int NewWidth = this.Size.Width; int MaxHeight = this.Size.Height; bool OnlyResizeIfWider = true; if (imageName.Length != 0) { if (!(File.Exists(imageName))) { imageName = Path.Combine(VWGContext.Current.Config.GetDirectory("Images"), "no_photo.jpg"); } // This allows us to resize the image. It prevents skewed images and // also vertically long images caused by trying to maintain the aspect // ratio on images who's height is larger than their width System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(imageName); // Prevent using images internal thumbnail FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); if (OnlyResizeIfWider) { if (FullsizeImage.Width <= NewWidth) { NewWidth = FullsizeImage.Width; } } int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width; if (NewHeight > MaxHeight) { // Resize with height instead NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height; NewHeight = MaxHeight; } System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero); // Clear handle to original file so that we can overwrite it if necessary FullsizeImage.Dispose(); // Save resized picture NewImage.Save(HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg); NewImage.Dispose(); } return(objGH); }
/// <summary> /// /// </summary> /// <param name="path"></param> /// <param name="rotate_flip"></param> /// <returns></returns> public static Bitmap RotateFlipImage(string path, RotateFlipType rotate_flip) { Rectangle destRect, srcRect; Bitmap bitmap = null; System.Drawing.Image img = null; img = System.Drawing.Image.FromFile(path); img.RotateFlip(rotate_flip); // Setup new bitmap bitmap = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb); bitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution); // Draw it Graphics g = Graphics.FromImage(bitmap); destRect = new Rectangle(0, 0, img.Width, img.Height); srcRect = new Rectangle(0, 0, img.Width, img.Height); g.DrawImage(img, destRect, srcRect, GraphicsUnit.Pixel); g.Dispose(); img.Dispose(); return(bitmap); }
void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs) { try { img = (Bitmap)eventArgs.Frame.Clone(); img.RotateFlip(RotateFlipType.RotateNoneFlipX); MemoryStream ms = new MemoryStream(); img.Save(ms, ImageFormat.Bmp); ms.Seek(0, SeekOrigin.Begin); BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = ms; bi.EndInit(); bi.Freeze(); Task.Run(() => { BitmapImage = bi; UpBitmap?.Invoke(this, new EventArgs()); }); //LocalWebCam.Stop(); } catch (Exception ex) { } }
protected void CreateImageFileWithBarcode(string scriptsHTML, string fileName, string code) { try { System.Drawing.PointF point = new System.Drawing.PointF(0, 0); System.Drawing.PointF _subPpoint = new System.Drawing.PointF(0, 0); System.Drawing.Image _image = System.Drawing.Image.FromFile(Server.MapPath("../Gallery/Contents/ticket.PNG")); _image = _image.GetThumbnailImage(755, 415, null, IntPtr.Zero); HtmlRender.RenderToImage(_image, scriptsHTML, point); System.Drawing.Image _barcode = CodeGenerator.CreateBarCode(code); //System.Drawing.Image _barcode = Code.CreateQrCode(code, 200); //_barcode = _barcode.GetThumbnailImage(150, 150, null, IntPtr.Zero); _barcode = _barcode.GetThumbnailImage(200, 70, null, IntPtr.Zero); HtmlRender.RenderToImage(_barcode, string.Empty, _subPpoint); _barcode.RotateFlip(RotateFlipType.Rotate270FlipX); Bitmap bitmap = new Bitmap(755, 415); using (Graphics graphics = Graphics.FromImage(bitmap)) { graphics.DrawImage(_image, 0, 0); graphics.DrawImage(_barcode, 630, 115); //graphics.DrawImage(_barcode, 550, 150); bitmap.Save(Server.MapPath("../Gallery/Ticket/" + fileName + ".PNG"), System.Drawing.Imaging.ImageFormat.Png); } } catch (Exception ex) { string a = ex.Message; } }
public void Rotate(System.Drawing.Image img) { // path = @"C:\ScannedFish\Fish.jpg"; // Debug.Log("Rotate 90: " + path); // System.Drawing.Image img = System.Drawing.Image.FromFile(path); img.RotateFlip(RotateFlipType.Rotate90FlipNone); }
public static void ImageResize(string inputPath, string fileName, string outFileName, int size, int quality) { try { using (System.Drawing.Image img = System.Drawing.Image.FromFile(Path.Combine(inputPath, fileName))) { foreach (var prop in img.PropertyItems) { if (prop.Id == 0x0112) //value of EXIF { int orientationValue = img.GetPropertyItem(prop.Id).Value[0]; RotateFlipType rotateFlipType = GetOrientationToFlipType(orientationValue); img.RotateFlip(rotateFlipType); break; } } using (var image = new Bitmap(img)) { int width, height; if (image.Width > image.Height) { width = size; height = Convert.ToInt32(image.Height * size / (double)image.Width); } else { width = Convert.ToInt32(image.Width * size / (double)image.Height); height = size; } var resized = new Bitmap(width, height); using (var graphics = Graphics.FromImage(resized)) { graphics.CompositingQuality = CompositingQuality.HighSpeed; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.CompositingMode = CompositingMode.SourceCopy; graphics.DrawImage(image, 0, 0, width, height); using (var output = File.Open(Path.Combine(inputPath, outFileName), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { var qualityParamId = Encoder.Quality; var encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(qualityParamId, quality); var codec = ImageCodecInfo.GetImageDecoders().FirstOrDefault(im => im.FormatID == ImageFormat.Png.Guid); resized.Save(output, codec, encoderParameters); output.Close(); output.Dispose(); graphics.Dispose(); resized.Dispose(); } } image.Dispose(); } img.Dispose(); } } catch { throw; } //return inputPath; }
void SpawnNewObj(string filePath) { isLoadingImage = true; spawnPos = spawnLocationTransforms[Random.Range(0, spawnLocationTransforms.Length)].position; GameObject newSpawnedObj = Instantiate(prefabSpawnObj, spawnPos, Quaternion.Euler(spawnRot)); currSpawnedObj = newSpawnedObj; currSpawnedObj.transform.position = new Vector3(currSpawnedObj.transform.position.x, currSpawnedObj.transform.position.y, currSpawnedObj.transform.position.z + zDistanceForDiffFish); zDistanceForDiffFish += 1f; currSpawnedObj.transform.parent = parentSpawnedObj; currSpawnedObj.name = filePath.Substring(dirPathLength); System.Drawing.Image img = System.Drawing.Image.FromFile(filePath); if (img.Height > 2000) { img.RotateFlip(RotateFlipType.Rotate90FlipNone); if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } img.Save(filePath); img.Dispose(); } currSpawnedObj.SetActive(false); StartCoroutine("GetImage", filePath); }
public Image ResizeImage( Image fullsizeImage, int newWidth ) { // Prevent using images internal thumbnail fullsizeImage.RotateFlip( System.Drawing.RotateFlipType.Rotate180FlipNone ); fullsizeImage.RotateFlip( System.Drawing.RotateFlipType.Rotate180FlipNone ); var newHeight = fullsizeImage.Height * newWidth / fullsizeImage.Width; var newImage = fullsizeImage.GetThumbnailImage( newWidth, newHeight, null, IntPtr.Zero ); // Clear handle to original file so that we can overwrite it if necessary fullsizeImage.Dispose(); // Save resized picture return newImage; }
void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs) { try { System.Drawing.Image img = (Bitmap)eventArgs.Frame.Clone(); img.RotateFlip(RotateFlipType.Rotate180FlipY); MemoryStream ms = new MemoryStream(); img.Save(ms, ImageFormat.Bmp); ms.Seek(0, SeekOrigin.Begin); BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = ms; bi.EndInit(); bi.Freeze(); Dispatcher.BeginInvoke(new ThreadStart(delegate { frameHolder.Source = bi; })); } catch (Exception ex) { } }
/// <summary> /// Instantiates a Robot object /// </summary> /// <param name="img">Image</param> /// <param name="imgOr">Cardinal direction</param> public Robot(Image img, Direction imgOr = Direction.North) { RoboImg = img; RoboImg.RotateFlip(RotateFlipType.Rotate180FlipNone); FaceDirection = imgOr; if (FaceDirection.Equals(Direction.East)) { RoboImg.RotateFlip(RotateFlipType.Rotate270FlipNone); } if (FaceDirection.Equals(Direction.South)) { RoboImg.RotateFlip(RotateFlipType.Rotate180FlipNone); } if (FaceDirection.Equals(Direction.West)) { RoboImg.RotateFlip(RotateFlipType.Rotate90FlipNone); } }
private void DrawImage(string filePath, string xueHao, string name, float xPosition, float yPosition) { System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(filePath); sourceImage.RotateFlip(RotateFlipType.Rotate90FlipXY); Bitmap newImage = new Bitmap(sourceImage, sourceImage.Width, sourceImage.Height); //newImage.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution); newImage.SetResolution(100, 100);//设置分辨率的大小 Graphics gh = Graphics.FromImage(newImage); gh.SmoothingMode = SmoothingMode.HighQuality; gh.DrawImage(newImage, new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), 0, 0, sourceImage.Width, sourceImage.Height, GraphicsUnit.Pixel); Font newFont = null; SizeF fontSize = new SizeF(); newFont = new Font("黑体", 22, FontStyle.Bold); fontSize = gh.MeasureString(xueHao, newFont); StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; SolidBrush brush = new SolidBrush(Color.White); gh.DrawString(xueHao, newFont, brush, new PointF(xPosition, yPosition), format); newImage.RotateFlip(RotateFlipType.Rotate270FlipXY); if (name.Length < 5) { gh.DrawString(AddSplit(name), newFont, brush, new PointF(sourceImage.Height - yPosition - 13, xPosition + 129), format); } newImage.Save(filePath, ImageFormat.Jpeg); gh.Dispose(); sourceImage.Dispose(); }
public static void FitFormToImage(string originalFile, string newFileAddress, int frameWidth, int frameHeight, bool flag) { System.IO.FileInfo imagefileinfo = new FileInfo(originalFile); if (!imagefileinfo.Exists) { return; } System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(originalFile); // Prevent using images internal thumbnail FullsizeImage.RotateFlip(RotateFlipType.Rotate180FlipNone); FullsizeImage.RotateFlip(RotateFlipType.Rotate180FlipNone); int newWidth, newHeight; if (FullsizeImage.Height > FullsizeImage.Width) { if (flag && FullsizeImage.Height < frameHeight) { FullsizeImage.Save(newFileAddress); return; } newHeight = frameHeight; newWidth = (int)(FullsizeImage.Width * ((float)newHeight / (float)FullsizeImage.Height)); } else { if (flag && FullsizeImage.Width < frameWidth) { FullsizeImage.Save(newFileAddress); return; } newWidth = frameWidth; newHeight = (int)(FullsizeImage.Height * ((float)newWidth / (float)FullsizeImage.Width)); } System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero); // Clear handle to original file so that we can overwrite it if necessary FullsizeImage.Dispose(); NewImage.Save(newFileAddress); NewImage.Dispose(); }
public static byte[] ResizeImage(System.Drawing.Image FullsizeImage, int NewWidth, int MaxHeight, ImageFormat imageFormat) { bool OnlyResizeIfWider = true; FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); if (OnlyResizeIfWider) { if (FullsizeImage.Width <= NewWidth) { NewWidth = FullsizeImage.Width; } } int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width; if (NewHeight > MaxHeight) { // Resize with height instead NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height; NewHeight = MaxHeight; } //System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero); Size newSize = new Size(NewWidth, NewHeight); using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height)) { using (Graphics canvas = Graphics.FromImage(newImage)) { canvas.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; canvas.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; canvas.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; canvas.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; /*canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;*/ canvas.DrawImage(FullsizeImage, new Rectangle(new Point(0, 0), newSize)); MemoryStream m = new MemoryStream(); newImage.Save(m, imageFormat); return(m.GetBuffer()); //return newImage; } } }
public ActionResult UploadImg(string imgbase64, int rotate, double w, double h) { ////图片类型 var imgtype = System.Drawing.Imaging.ImageFormat.Jpeg; ////选择角度 var imgrotate = RotateFlipType.RotateNoneFlipNone; ////图片后缀名 var imghz = "jpg"; ////设置图片类型 if (imgbase64.IndexOf("data:image/jpeg;base64,") > -1) { imgtype = System.Drawing.Imaging.ImageFormat.Jpeg; imghz = "jpg"; } if (imgbase64.IndexOf("data:image/png;base64,") > -1) { imgtype = System.Drawing.Imaging.ImageFormat.Png; imghz = "png"; } ////设置选择角度 switch (rotate) { case 1: imgrotate = RotateFlipType.Rotate90FlipNone; break; case 2: imgrotate = RotateFlipType.Rotate180FlipNone; break; case 3: imgrotate = RotateFlipType.Rotate270FlipNone; break; } ////定义图片名称 string newFileName = System.Web.HttpContext.Current.Server.MapPath("/Areas/Benz/uploadFile/" + ApplicationContext.Current.LogonInfo.Openid + GetTimeStamp() + "." + imghz); string newzoomFileName = System.Web.HttpContext.Current.Server.MapPath("/Areas/Benz/uploadFilezoom/" + ApplicationContext.Current.LogonInfo.Openid + GetTimeStamp() + "." + imghz); Session["picurl"] = "/Areas/Benz/uploadFile/" + ApplicationContext.Current.LogonInfo.Openid + GetTimeStamp() + "." + imghz; //ViewBag.picurl = "/uploadFile/1." + imghz; ////把图片数据格式化到字节数组 byte[] arr = Convert.FromBase64String(imgbase64.Replace("data:image/jpeg;base64,", "").Replace("data:image/png;base64,", "")); ////读取字节数到文件流 MemoryStream ms = new MemoryStream(arr); ////保存图片 System.Drawing.Image newImage = System.Drawing.Image.FromStream(ms); ////旋转角度 newImage.RotateFlip(imgrotate); ////保存 newImage.Save(newFileName, imgtype); ////关闭流 newImage.Dispose(); //ImageHandler.ZoomImage(ms, newzoomFileName, w * 0.3, h * 0.3); ImageHandler.GetPicThumbnail(newFileName, newzoomFileName, 10); return(Json(ApplicationContext.Current.LogonInfo.Openid + GetTimeStamp() + "." + imghz)); }
public static byte[] byteArrayRotate(byte[] byteArrayIn, string contentType, RotateFlipType rft) { System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(byteArrayIn); System.Drawing.Image returnImage = System.Drawing.Image.FromStream(memoryStream); memoryStream.Close(); returnImage.RotateFlip(rft); return(imageToByteArray(returnImage, contentType)); }
public static void printImage(BitmapImage bi) { JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bi)); FileStream files = new FileStream("1.jpg", FileMode.Create, FileAccess.ReadWrite); encoder.Save(files); files.Close(); PrintDocument pd = new PrintDocument(); pd.DefaultPageSettings.PrinterSettings.PrinterName = "Canon SELPHY CP1200"; PaperSize psize = new PaperSize(); foreach (PaperSize i in pd.PrinterSettings.PaperSizes) { if (i.PaperName == "P 无边距 100x148mm 4x6\"") //无边距可正常居中,有边距0,0点位置需考虑边距 { psize = i; break; } Console.WriteLine(i.PaperName); } pd.DefaultPageSettings.PaperSize = psize; pd.PrintPage += (s, args) => { System.Drawing.Image i = System.Drawing.Image.FromFile("1.jpg"); System.Drawing.Rectangle m = args.PageBounds; if (i.Width < i.Height) { i.RotateFlip(RotateFlipType.Rotate90FlipNone); } if (i.Width >= i.Height) { if ((double)i.Width / (double)i.Height <= (double)m.Width / (double)m.Height) { int w = (int)((double)i.Width / (double)i.Height * (double)m.Height); int dx = (m.Width - w) / 2; m.X = dx; m.Y = 0; m.Width = w; } else { int h = (int)((double)i.Height / (double)i.Width * (double)m.Width); int dy = (m.Height - h) / 2; m.X = 0; m.Y = dy; m.Height = h; } } args.Graphics.DrawImage(i, m); }; pd.Print(); }
public static void ResizeImage(string originalFile, string newFile, int newWidth, int maxHeight, bool onlyResizeIfWider) { try { System.Drawing.Image fullsizeImage = System.Drawing.Image.FromFile(originalFile); string thumPath = Path.Combine(Path.GetDirectoryName(originalFile), "thumb"); if (!Directory.Exists(thumPath)) { Directory.CreateDirectory(thumPath); } // Prevent using images internal thumbnail fullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); fullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); if (onlyResizeIfWider) { if (fullsizeImage.Width <= newWidth) { newWidth = fullsizeImage.Width; } } int NewHeight = fullsizeImage.Height * newWidth / fullsizeImage.Width; if (NewHeight > maxHeight) { // Resize with height instead newWidth = fullsizeImage.Width * maxHeight / fullsizeImage.Height; NewHeight = maxHeight; } System.Drawing.Image NewImage = fullsizeImage.GetThumbnailImage(newWidth, NewHeight, null, IntPtr.Zero); // Clear handle to original file so that we can overwrite it if necessary fullsizeImage.Dispose(); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); // Save resized picture NewImage.Save(newFile); } catch (Exception) { throw; } }
public async Task RotateImage(string imagePath) { Console.WriteLine($"Rotating {imagePath} image by 90 degrees..."); using (System.Drawing.Image img = System.Drawing.Image.FromFile(imagePath)) { img.RotateFlip(RotateFlipType.Rotate90FlipX); img.Save($"{imagePath.Split('.')[0]}Rotated.jpg"); } }
public static Image ResizeImage(Image image, int width, int height, bool onlyResizeIfWider) { // Prevent using images internal thumbnail image.RotateFlip(RotateFlipType.Rotate180FlipNone); image.RotateFlip(RotateFlipType.Rotate180FlipNone); if (onlyResizeIfWider) if (image.Width <= width) width = image.Width; var newHeight = image.Height * width / image.Width; if (newHeight > height) { // Resize with height instead width = image.Width * height / image.Height; newHeight = height; } return image.GetThumbnailImage(width, newHeight, null, IntPtr.Zero); }
private static void ModifyImage(Image i) { i.RotateFlip(RotateFlipType.Rotate180FlipNone); using (var g = Graphics.FromImage(i)) { g.SmoothingMode = SmoothingMode.AntiAlias; g.DrawString("To the cloud!", new Font("Arial", 42, FontStyle.Bold), SystemBrushes.ActiveCaptionText, new Point(0, 0)); } }
/*public static Image AppendBorder(Image original, int borderWidth) { var borderColor = Color.White; var newSize = new Size( original.Width + borderWidth * 2, original.Height + borderWidth * 2); var img = new Bitmap(newSize.Width, newSize.Height); var g = Graphics.FromImage(img); g.Clear(borderColor); g.DrawImage(original, new Point(borderWidth, borderWidth)); g.Dispose(); return img; }*/ //Image resizing public static System.Drawing.Image ResizeImage(Image Image, int maxWidth, int maxHeight) { //return FixedSize(Image, maxWidth, maxHeight, true); int width = Image.Width; int height = Image.Height; if (width > maxWidth || height > maxHeight) { //The flips are in here to prevent any embedded image thumbnails -- usually from cameras //from displaying as the thumbnail image later, in other words, we want a clean //resize, not a grainy one. Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX); Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX); float ratio = 0; if (width > height) { ratio = (float)width / (float)height; width = maxWidth; height = Convert.ToInt32(Math.Round((float)width / ratio)); } else { ratio = (float)height / (float)width; height = maxHeight; width = Convert.ToInt32(Math.Round((float)height / ratio)); } //Rectangle destRect = new Rectangle(0, 0, maxWidth, maxHeight); //// Draw image to screen. //e.Graphics.DrawImage(newImage, destRect); //return the resized image return Image.GetThumbnailImage(width, height, null, IntPtr.Zero); } //return the original resized image return Image; }
public static Image ResizeImage(Image FullsizeImage, int NewWidth, int MaxHeight, bool OnlyResizeIfWider) { // Prevent using images internal thumbnail FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); if (OnlyResizeIfWider) { if (FullsizeImage.Width <= NewWidth) { NewWidth = FullsizeImage.Width; } } int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width; if (NewHeight > MaxHeight) { // Resize with height instead NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height; NewHeight = MaxHeight; } return FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero); }
public MyPictureBox(Image img,pbInfo _pbInfo) { InitializeComponent(); pictureBox.Image = img; pbInfo = _pbInfo; label.Text = pbInfo.Server; pbInfo.pbInfoChanged += (a, b) => { switch(b.PropertyName) { case "FilePath": RefreshImage(pbInfo.FilePath); break; case "Rotation": comboBoxRotate.Text = pbInfo.Rotate.ToString(); break; case "Server": label.Text = pbInfo.Server; label.BackColor = System.Drawing.Color.Transparent; label.ForeColor = System.Drawing.Color.Black; break; case "Error": if (pbInfo.Error != null) { label.Text = pbInfo.Error.Message; label.BackColor = System.Drawing.Color.Red; label.ForeColor = System.Drawing.Color.White; } break; } }; comboBoxRotate.TextChanged += (a, b) => { PointF pf = new PointF(2, img.Height - 20); if (pbInfo.Rotate > 0) { img.RotateFlip(Stuff.getRotateFlipType(pbInfo.Rotate)); if (pbInfo.Rotate == 90 || pbInfo.Rotate == 270) { pictureBox.Size = new Size(240, 320); pf = new PointF(2, img.Height - 20); } } else { pictureBox.Size = new Size(320, 240); } }; }
public static void RotateImage(Image imageToRotate, ImageRotationStyle style) { if (imageToRotate != null) { RotateFlipType flipType; switch (style) { case ImageRotationStyle.CCW: { flipType = RotateFlipType.Rotate90FlipXY; break; } default: { flipType = RotateFlipType.Rotate270FlipXY; break; } } imageToRotate.RotateFlip(flipType); } }
public override Image Apply(Image img) { RotateFlipType flipType = RotateFlipType.RotateNoneFlipNone; if (Horizontally && Vertically) { flipType = RotateFlipType.RotateNoneFlipXY; } else if (Horizontally) { flipType = RotateFlipType.RotateNoneFlipX; } else if (Vertically) { flipType = RotateFlipType.RotateNoneFlipY; } if (flipType != RotateFlipType.RotateNoneFlipNone) { img.RotateFlip(flipType); } return img; }
public static void ImageRotation(Image img) { if (Array.IndexOf(img.PropertyIdList, 274) > -1) { var orientation = (int)img.GetPropertyItem(274).Value[0]; switch (orientation) { case 1: // No rotation required. break; case 2: img.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case 3: img.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case 4: img.RotateFlip(RotateFlipType.Rotate180FlipX); break; case 5: img.RotateFlip(RotateFlipType.Rotate90FlipX); break; case 6: img.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case 7: img.RotateFlip(RotateFlipType.Rotate270FlipX); break; case 8: img.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } // This EXIF data is now invalid and should be removed. //img.RemovePropertyItem(274); } }
void BtnRotateClick(object sender, EventArgs e) { listNum = SelectedDocnum(); pic = Image.FromStream(_Doc.Pages[listNum].ImageStream ); // pic = Image.FromFile(imgLst[listNum].FileName); //srcBmp.SelectActiveFrame(FrameDimension.Page, item.ImageIndex) //PictureBox1.Image = ResizeImage(srcBmp, New Size(PictureBox1.Width, PictureBox1.Height)) pic.RotateFlip(RotateFlipType.Rotate90FlipNone); pictureBox1.Width = pic.Width; pictureBox1.Height = pic.Height; pictureBox1.Image = pic; ms = new MemoryStream(); pic.Save(ms, ImageFormat.Png); _Doc.Pages[listNum].ImageStream = ms; imageList1.Images[listNum] = new Bitmap(pic, 128, 128); listView1.RedrawItems(listNum, listNum, false); }
void timer1_Tick(object sender, EventArgs e) { try { ip = this.GetBitMap(); image = new Bitmap(this.Width, this.Height, this.Stride, PixelFormat.Format24bppRgb, ip); image.RotateFlip(RotateFlipType.RotateNoneFlipY); if (camimage != null) { camimage(image); } } catch { Console.WriteLine("Grab bmp failed"); timer1.Enabled = false; this.CloseInterfaces(); System.Windows.Forms.CustomMessageBox.Show("Problem with capture device, grabbing frame took longer than 5 sec"); } }
private void Form1_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { // 리셋 case Keys.Space: Attr = new ImageAttributes(); Src = Image.FromFile("오솔길.jpg"); Threshold = 0.5f; Gamma = 1.0f; Bright = 0.0f; break; // 시계 방향 회전 case Keys.Right: Src.RotateFlip(RotateFlipType.Rotate90FlipNone); break; // 반시계 방향 회전 case Keys.Left: Src.RotateFlip(RotateFlipType.Rotate270FlipNone); break; // 수평 뒤집기 case Keys.Up: Src.RotateFlip(RotateFlipType.RotateNoneFlipX); break; // 수직 뒤집기 case Keys.Down: Src.RotateFlip(RotateFlipType.RotateNoneFlipY); break; // 스레시 홀드 증감 case Keys.Q: if (Threshold < 1) Threshold += 0.1f; Attr.SetThreshold(Threshold); break; case Keys.W: if (Threshold > 0) Threshold -= 0.1f; Attr.SetThreshold(Threshold); break; // 감마 증감 case Keys.E: if (Gamma < 5.0) Gamma += 0.1f; Attr.SetGamma(Gamma); break; case Keys.R: if (Gamma > 0.1) Gamma -= 0.1f; Attr.SetGamma(Gamma); break; // 밝게 case Keys.D1: if (Bright < 1.0f) Bright += 0.1f; float[][] M1 = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 1.0f, 0.0f}, new float[] {Bright, Bright, Bright, 0.0f, 1.0f}, }; ColorMatrix Mat1 = new ColorMatrix(M1); Attr.SetColorMatrix(Mat1); break; // 어둡게 case Keys.D2: if (Bright > -1.0f) Bright -= 0.1f; float[][] M2 = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 1.0f, 0.0f}, new float[] {Bright, Bright, Bright, 0.0f, 1.0f}, }; ColorMatrix Mat2 = new ColorMatrix(M2); Attr.SetColorMatrix(Mat2); break; // 반전 case Keys.D3: float[][] M3 = { new float[] {-1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, -1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, -1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 1.0f, 0.0f}, new float[] {1.0f, 1.0f, 1.0f, 0.0f, 1.0f}, }; ColorMatrix Mat3 = new ColorMatrix(M3); Attr.SetColorMatrix(Mat3); break; // 그레이 스케일 case Keys.D4: float[][] M4 = { new float[] {0.299f, 0.299f, 0.299f, 0.0f, 0.0f}, new float[] {0.587f, 0.587f, 0.587f, 0.0f, 0.0f}, new float[] {0.114f, 0.114f, 0.114f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 1.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}, }; ColorMatrix Mat4 = new ColorMatrix(M4); Attr.SetColorMatrix(Mat4); break; case Keys.D5: ColorMap[] Map = new ColorMap[1]; Map[0] = new ColorMap(); Map[0].OldColor = Color.White; Map[0].NewColor = Color.Blue; Attr.SetRemapTable(Map); break; } Invalidate(); }
static Settings() { IslandImage = new Bitmap(typeof(GameTabPage), "Island.png"); IslandImage.RotateFlip(RotateFlipType.Rotate180FlipNone); }
/// <summary> /// Creates a thumbnail of the specified image /// </summary> /// <param name="aDrawingImage">The source System.Drawing.Image</param> /// <param name="aThumbTargetPath">Filename of the thumbnail to create</param> /// <param name="aThumbWidth">Maximum width of the thumbnail</param> /// <param name="aThumbHeight">Maximum height of the thumbnail</param> /// <param name="aRotation"> /// 0 = no rotate /// 1 = rotate 90 degrees /// 2 = rotate 180 degrees /// 3 = rotate 270 degrees /// </param> /// <param name="aFastMode">Use low quality resizing without interpolation suitable for small thumbnails</param> /// <returns>Whether the thumb has been successfully created</returns> public static bool CreateThumbnail(Image aDrawingImage, string aThumbTargetPath, int aThumbWidth, int aThumbHeight, int aRotation, bool aFastMode) { bool result = false; if (string.IsNullOrEmpty(aThumbTargetPath) || aThumbHeight <= 0 || aThumbHeight <= 0) return false; Bitmap myBitmap = null; Image myTargetThumb = null; try { switch (aRotation) { case 1: aDrawingImage.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case 2: aDrawingImage.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case 3: aDrawingImage.RotateFlip(RotateFlipType.Rotate270FlipNone); break; default: break; } int iWidth = aThumbWidth; int iHeight = aThumbHeight; float fAR = (aDrawingImage.Width) / ((float)aDrawingImage.Height); if (aDrawingImage.Width > aDrawingImage.Height) iHeight = (int)Math.Floor((((float)iWidth) / fAR)); else iWidth = (int)Math.Floor((fAR * ((float)iHeight))); try { Utils.FileDelete(aThumbTargetPath); } catch (Exception ex) { Log.Error("Picture: Error deleting old thumbnail - {0}", ex.Message); } if (aFastMode) { Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback); myBitmap = new Bitmap(aDrawingImage, iWidth, iHeight); myTargetThumb = myBitmap.GetThumbnailImage(iWidth, iHeight, myCallback, IntPtr.Zero); } else { PixelFormat format = aDrawingImage.PixelFormat; switch (format) { case PixelFormat.Format1bppIndexed: case PixelFormat.Format4bppIndexed: case PixelFormat.Format8bppIndexed: case PixelFormat.Undefined: case PixelFormat.Format16bppArgb1555: case PixelFormat.Format16bppGrayScale: format = PixelFormat.Format32bppRgb; break; } myBitmap = new Bitmap(iWidth, iHeight, format); //myBitmap.SetResolution(aDrawingImage.HorizontalResolution, aDrawingImage.VerticalResolution); using (Graphics g = Graphics.FromImage(myBitmap)) { g.CompositingQuality = Thumbs.Compositing; g.InterpolationMode = Thumbs.Interpolation; g.SmoothingMode = Thumbs.Smoothing; g.DrawImage(aDrawingImage, new Rectangle(0, 0, iWidth, iHeight)); myTargetThumb = myBitmap; } } if (MediaPortal.Player.g_Player.Playing) Thread.Sleep(30); result = SaveThumbnail(aThumbTargetPath, myTargetThumb); } catch (Exception) { result = false; } finally { if (myTargetThumb != null) myTargetThumb.SafeDispose(); if (myBitmap != null) myBitmap.SafeDispose(); } if (result && Utils.IsFileExistsCacheEnabled()) { Log.Debug("CreateThumbnail : FileExistsInCache updated with new file: {0}", aThumbTargetPath); Utils.DoInsertExistingFileIntoCache(aThumbTargetPath); } return result; }
/// <summary> /// Returns a resized image that fits in width and height /// </summary> /// <returns></returns> private Image getResizedImage(Image imageToResize) { // Do not increase image size if (imageToResize.Width < width && imageToResize.Height < height) { return imageToResize; } // Prevent using images internal thumbnail imageToResize.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); imageToResize.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); // Find out the scale factor for the resize double widthScale = 0; double heightScale = 0; if (imageToResize.Width > 0) { widthScale = (double)width / (double)imageToResize.Width; } if (imageToResize.Height > 0) { heightScale = (double)height / (double)imageToResize.Height; } double scale = Math.Min(widthScale, heightScale); // Resize the image Image ResizedImage = new Bitmap((int)(imageToResize.Width * scale), (int)(imageToResize.Height * scale)); using (Graphics graphicsHandle = Graphics.FromImage(ResizedImage)) { graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic; graphicsHandle.DrawImage(imageToResize, 0, 0, ResizedImage.Width, ResizedImage.Height); } return ResizedImage; }
/// <summary> /// The image to be displayed in the Edit Portion. /// </summary> public void SetImageToDraw() { if(Owner == null) return; index = Owner.SelectedIndex; if((index > -1)) { int imageindex = ((ImageComboBoxItem)Owner.Items [index ]).ImageIndex; if(imageindex != -1) { CurrentIcon = new Bitmap(Owner.ImageList.Images [imageindex],Owner.ItemHeight,Owner.ItemHeight); if(Owner.RightToLeft == RightToLeft.Yes) CurrentIcon.RotateFlip(RotateFlipType.RotateNoneFlipX); Margin = CurrentIcon.Width+2; // required width to display the image correctly } else { CurrentIcon = null; Margin = 0; } } }
/// <summary> /// Given an exif orientation, rotates the passed in image accordingly /// </summary> /// <param name="orientation"></param> /// <param name="image"></param> public static bool RotateFromExifOrientation(ExifOrientation orientation, Image image) { switch (orientation) { case ExifOrientation.Rotate90CW: image.RotateFlip(RotateFlipType.Rotate90FlipNone); return true; case ExifOrientation.Rotate270CW: image.RotateFlip(RotateFlipType.Rotate270FlipNone); return true; case ExifOrientation.Normal: case ExifOrientation.Unknown: return false; } return false; }
public override Image ApplyTransformation(Image image) { image.RotateFlip(this.Direction); return image; }
/// <summary> /// Effectue un RotateFlip selon l'angle /// </summary> /// <param name="image"></param> /// <returns></returns> public Image RotateImage(Image image) { Log.Info("Rotation de l'image : " + p_currentAngle.ToString()); if (p_currentAngle.Equals(90.0F)) { image.RotateFlip(RotateFlipType.Rotate90FlipNone); } if (p_currentAngle.Equals(180.0F)) { image.RotateFlip(RotateFlipType.Rotate180FlipNone); } if (p_currentAngle.Equals(-90.0F)) { image.RotateFlip(RotateFlipType.Rotate270FlipNone); } return image; }
private RotateFlipType PerformRotation(Image image) { var folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase); var path = Path.Combine(folder, "RecognizeImage.exe"); path = path.Substring(6); // create for process with different tasks var processStartInfo = new ProcessStartInfo(path); processStartInfo.CreateNoWindow = true; processStartInfo.UseShellExecute = false; processStartInfo.Arguments = "0"; Process.Start(processStartInfo); processStartInfo.Arguments = "90"; Process.Start(processStartInfo); processStartInfo.Arguments = "180"; Process.Start(processStartInfo); processStartInfo.Arguments = "270"; Process.Start(processStartInfo); isImagesParsingDone.WaitOne(); var realDirection = RotateFlipType.RotateNoneFlipNone; var result = metrics[realDirection]; var words = imagesWords[realDirection]; if (result < metrics[RotateFlipType.Rotate90FlipNone]) { realDirection = RotateFlipType.Rotate90FlipNone; result = metrics[realDirection]; words = imagesWords[realDirection]; } if (result < metrics[RotateFlipType.Rotate180FlipNone]) { realDirection = RotateFlipType.Rotate180FlipNone; result = metrics[realDirection]; words = imagesWords[realDirection]; } if (result < metrics[RotateFlipType.Rotate270FlipNone]) { realDirection = RotateFlipType.Rotate270FlipNone; result = metrics[realDirection]; words = imagesWords[realDirection]; } if (realDirection == RotateFlipType.Rotate180FlipNone && ((result - metrics[RotateFlipType.RotateNoneFlipNone]) / (double)result * 100 < 7.5)) { realDirection = RotateFlipType.RotateNoneFlipNone; } if (words.Count < 10) { realDirection = RotateFlipType.RotateNoneFlipNone; } if (realDirection != RotateFlipType.RotateNoneFlipNone) { image.RotateFlip(realDirection); } return realDirection; }
private void AddIcon(string name, Image img) { if (img.Width > 256 || img.Height > 256) { var widthIsConstraining = img.Width > img.Height; // Prevent using images internal thumbnail img.RotateFlip(RotateFlipType.Rotate180FlipNone); img.RotateFlip(RotateFlipType.Rotate180FlipNone); var newWidth = widthIsConstraining ? 256 : img.Width*256/img.Height; var newHeight = widthIsConstraining ? img.Height*256/img.Width : 256; var newImage = img.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero); img.Dispose(); img = newImage; } var tmpImage = (name + ".png").GetFileInTempFolder(); img.Save(tmpImage, ImageFormat.Png); var icon = wix.Product.Add("Binary"); icon.Attributes.Id = "ICON_{0}".format(name); icon.Attributes.SourceFile = tmpImage; }
//Image resizing public static Image ResizeImage(int maxWidth, int maxHeight, Image Image) { int width = Image.Width; int height = Image.Height; //The flips are in here to prevent any embedded image thumbnails -- usually from cameras //from displaying as the thumbnail image later, in other words, we want a clean //resize, not a grainy one. Image.RotateFlip (RotateFlipType.Rotate180FlipX); Image.RotateFlip (RotateFlipType.Rotate180FlipX); float ratio = 0; if (width > height) { ratio = (float)width / (float)height; width = maxWidth; height = Convert.ToInt32 (Math.Round((float)width / ratio)); } else { ratio = (float)height / (float)width; height = maxHeight; width = Convert.ToInt32 (Math.Round((float)height / ratio)); } //return the resized image return Image.GetThumbnailImage (width, height, null, IntPtr.Zero); }
public static Image GetRotatedImage(Image image, int degrees) { image.RotateFlip(RotateFlipType.Rotate90FlipNone); return new Bitmap(image); }
/// <summary> /// 画像データ生成 /// </summary> /// <param name="width">出力幅</param> /// <param name="height">出力高さ</param> /// <param name="src">元画像データ出力バッファ</param> /// <returns>結果画像</returns> public Image GenerateImages(int width, int height, out Image src) { src = Image.FromFile(Path); switch (Rotate) { case PageRotate.Rotate0: src.RotateFlip(RotateFlipType.RotateNoneFlipNone); break; case PageRotate.Rotate90: src.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case PageRotate.Rotate180: src.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case PageRotate.Rotate270: src.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } if (Format < 0) { switch (src.PixelFormat) { default: // フルカラー Format = Page.PageFormat.FullColor; break; case PixelFormat.Format8bppIndexed: // 8bitグレイスケール Format = Page.PageFormat.Gray8bit; break; case PixelFormat.Format16bppGrayScale: // 8bitグレイスケールにしちゃう Format = Page.PageFormat.Gray8bit; break; case PixelFormat.Format4bppIndexed: // 4bitグレイスケール Format = Page.PageFormat.Gray4bit; break; case PixelFormat.Format1bppIndexed: // 白黒 Format = Page.PageFormat.Mono; break; } } int srcWidth = src.Width * (100 - clipLeft - (100 - clipRight)) / 100; int srcHeight = src.Height * (100 - clipTop - (100 - clipBottom)) / 100; if (srcWidth / (double)width < srcHeight / (double)height) { double d = srcHeight / (double)height; width = (int)(srcWidth / d); } else { double d = srcWidth / (double)width; height = (int)(srcHeight / d); } if (width <= 0 || height <= 0) { return null; } // コントラスト指定 ImageAttributes attr = new ImageAttributes(); float contrast = this.contrast + 1.0f; float[][] array = { new float[] {contrast, 0, 0, 0, 0}, // red new float[] {0, contrast, 0, 0, 0}, // green new float[] {0, 0, contrast, 0, 0}, // blue new float[] {0, 0, 0, 1, 0}, // alpha new float[] {(1.0f - contrast) * 0.5f, (1.0f - contrast) * 0.5f, (1.0f - contrast) * 0.5f, 0, 1} // transform }; attr.SetColorMatrix(new ColorMatrix(array), ColorMatrixFlag.Default, ColorAdjustType.Bitmap); attr.SetGamma(1.0f, ColorAdjustType.Bitmap); // ビットマップ生成 Image target = new Bitmap(width, height); Graphics g = Graphics.FromImage(target); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(src, new Rectangle(0, 0, width, height), src.Width * clipLeft / 100, src.Height * clipTop / 100, srcWidth, srcHeight, GraphicsUnit.Pixel, attr); g.Dispose(); if (Format > Page.PageFormat.FullColor) { // 太字化はこの段階で反映 target = ConvertFormat(Boldize((Bitmap)target, this.bold), Format); } return target; }
public MarioDeathStyleObject(GameEnemy ge) : base(ge.Location,ge.DrawSize) { Location = ge.Location; SizeF ObjSize = ge.GetSize(); usedrawimage = new Bitmap((int)(ObjSize.Width * 2), (int)(ObjSize.Height * 2)); Graphics gdraw = Graphics.FromImage(usedrawimage); gdraw.Clear(Color.Transparent); //move it temporarity... ge.Location = new PointF(usedrawimage.Width / 2, usedrawimage.Height / 2); ge.Draw(gdraw); //move back ge.Location = Location; //flip the image vertically. usedrawimage.RotateFlip(RotateFlipType.RotateNoneFlipY); //now, set speed to our defaults. InitSpeeds(); if (ge is IMovingObject) { Velocity = new PointF((ge as IMovingObject).Velocity.X / 2, Velocity.Y); } }
/// <summary> /// Creates a thumbnail of the specified image /// </summary> /// <param name="aDrawingImage">The source System.Drawing.Image</param> /// <param name="aThumbTargetPath">Filename of the thumbnail to create</param> /// <param name="aThumbWidth">Maximum width of the thumbnail</param> /// <param name="aThumbHeight">Maximum height of the thumbnail</param> /// <param name="aRotation"> /// 0 = no rotate /// 1 = rotate 90 degrees /// 2 = rotate 180 degrees /// 3 = rotate 270 degrees /// </param> /// <param name="aFastMode">Use low quality resizing without interpolation suitable for small thumbnails</param> /// <returns>Whether the thumb has been successfully created</returns> private static bool CreateThumbnail(Image aDrawingImage, string aThumbTargetPath, int aThumbWidth, int aThumbHeight, int aRotation, bool aFastMode) { if (string.IsNullOrEmpty(aThumbTargetPath) || aThumbHeight <= 0 || aThumbHeight <= 0) return false; Bitmap myBitmap = null; Image myTargetThumb = null; try { switch (aRotation) { case 1: aDrawingImage.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case 2: aDrawingImage.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case 3: aDrawingImage.RotateFlip(RotateFlipType.Rotate270FlipNone); break; default: break; } int iWidth = aThumbWidth; int iHeight = aThumbHeight; float fAR = (aDrawingImage.Width) / ((float)aDrawingImage.Height); if (aDrawingImage.Width > aDrawingImage.Height) iHeight = (int)Math.Floor((((float)iWidth) / fAR)); else iWidth = (int)Math.Floor((fAR * ((float)iHeight))); /*try { Utils.FileDelete(aThumbTargetPath); } catch (Exception ex) { Log.Error("Picture: Error deleting old thumbnail - {0}", ex.Message); }*/ if (aFastMode) { Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback); myBitmap = new Bitmap(aDrawingImage, iWidth, iHeight); myTargetThumb = myBitmap.GetThumbnailImage(iWidth, iHeight, myCallback, IntPtr.Zero); } else { myBitmap = new Bitmap(iWidth, iHeight, aDrawingImage.PixelFormat); //myBitmap.SetResolution(aDrawingImage.HorizontalResolution, aDrawingImage.VerticalResolution); using (Graphics g = Graphics.FromImage(myBitmap)) { g.CompositingQuality = Thumbs.Compositing; g.InterpolationMode = Thumbs.Interpolation; g.SmoothingMode = Thumbs.Smoothing; g.DrawImage(aDrawingImage, new Rectangle(0, 0, iWidth, iHeight)); myTargetThumb = myBitmap; } } return SaveThumbnail(aThumbTargetPath, myTargetThumb); } catch (Exception) { return false; } finally { if (myTargetThumb != null) myTargetThumb.Dispose(); if (myBitmap != null) myBitmap.Dispose(); } }