public static Bitmap GetBitmap(Image mainImg, Size size, UserPhotoThumbnailSettings thumbnailSettings) { var thumbnailBitmap = new Bitmap(size.Width, size.Height); var scaleX = size.Width / (1.0 * thumbnailSettings.Size.Width); var scaleY = size.Height / (1.0 * thumbnailSettings.Size.Height); var rect = new Rectangle(-(int)(scaleX * (1.0 * thumbnailSettings.Point.X)), -(int)(scaleY * (1.0 * thumbnailSettings.Point.Y)), (int)(scaleX * mainImg.Width), (int)(scaleY * mainImg.Height)); using (var graphic = Graphics.FromImage(thumbnailBitmap)) { graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; graphic.DrawImage(mainImg, rect); using var wrapMode = new System.Drawing.Imaging.ImageAttributes(); wrapMode.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY); graphic.DrawImage(mainImg, rect, 0, 0, mainImg.Width, mainImg.Height, GraphicsUnit.Pixel, wrapMode); } return(thumbnailBitmap); }
void Command_Image_Size(ImageSizeDialog.Result result) { var variables = GetVariables(); var width = new NEExpression(result.WidthExpression).Evaluate <int>(variables); var height = new NEExpression(result.HeightExpression).Evaluate <int>(variables); var bitmap = GetBitmap(); var resultBitmap = new System.Drawing.Bitmap(width, height, bitmap.PixelFormat); resultBitmap.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution); using (var graphics = System.Drawing.Graphics.FromImage(resultBitmap)) { graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphics.InterpolationMode = result.InterpolationMode; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; using (var wrapMode = new System.Drawing.Imaging.ImageAttributes()) { wrapMode.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY); graphics.DrawImage(bitmap, new System.Drawing.Rectangle(0, 0, width, height), 0, 0, bitmap.Width, bitmap.Height, System.Drawing.GraphicsUnit.Pixel, wrapMode); } } Replace(new List <Range> { FullRange }, new List <string> { Coder.BitmapToString(resultBitmap) }); SetSelections(new List <Range> { BeginRange }); }
private void browse_btn_Click(object sender, EventArgs e) { using var OFD = new OpenFileDialog(); if (OFD.ShowDialog() == DialogResult.OK) // Test result. { targetFileName = OFD.FileName; using (Image tmpImg = Image.FromFile(targetFileName)) { var destRect = new Rectangle(0, 0, 502, 325); var destImage = new Bitmap(502, 325); destImage.SetResolution(tmpImg.HorizontalResolution, tmpImg.VerticalResolution); using (var graphics = Graphics.FromImage(destImage)) { graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; using var wrapMode = new System.Drawing.Imaging.ImageAttributes(); wrapMode.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY); graphics.DrawImage(tmpImg, destRect, 0, 0, tmpImg.Width, tmpImg.Height, GraphicsUnit.Pixel, wrapMode); } pictureBox.Image = destImage; } this.Height = maxHeight; GF.enableBtn(save_btn, Color.Green); this.CenterToScreen(); } }
// from https://stackoverflow.com/questions/1922040/how-to-resize-an-image-c-sharp public static Bitmap ResizeImage(this Image image, int width, int height, System.Drawing.Drawing2D.InterpolationMode interm = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic ) { if (width <= 0) { width = image.Width; } if (height <= 0) { height = image.Height; } var destRect = new Rectangle(0, 0, width, height); var destImage = new Bitmap(width, height); destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (var graphics = Graphics.FromImage(destImage)) { graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphics.InterpolationMode = interm; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; using (var wrapMode = new System.Drawing.Imaging.ImageAttributes()) { wrapMode.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY); graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); } } return(destImage); }
/// <summary> /// Resize the image to the specified width and height. /// </summary> /// <param name="image">The image to resize.</param> /// <param name="width">The width to resize to.</param> /// <param name="height">The height to resize to.</param> /// <returns>The resized image.</returns> public static Bitmap ResizeImage(Image image, int width, int height) { var destRect = new Rectangle(0, 0, width, height); var destImage = new Bitmap(width, height); destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (var graphics = Graphics.FromImage(destImage)) { //Want a high quality resize graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; using (var wrapMode = new System.Drawing.Imaging.ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); } } return(destImage); }
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Rectangle colorBoxOuter = new Rectangle( this.ClientRectangle.X + this.pickerSize, this.ClientRectangle.Y + this.pickerSize, this.ClientRectangle.Width - this.pickerSize * 2 - 1, this.ClientRectangle.Height - this.pickerSize * 2 - 1); Rectangle colorBoxInner = new Rectangle( colorBoxOuter.X + 1, colorBoxOuter.Y + 1, colorBoxOuter.Width - 2, colorBoxOuter.Height - 2); Rectangle colorArea = this.ColorAreaRectangle; int pickerVisualPos = colorArea.Y + (int)Math.Round((1.0f - this.pickerPos) * colorArea.Height); if (this.min.A < 255 || this.max.A < 255) { e.Graphics.FillRectangle(new HatchBrush(HatchStyle.LargeCheckerBoard, Color.LightGray, Color.Gray), colorArea); } System.Drawing.Imaging.ImageAttributes colorAreaImageAttr = new System.Drawing.Imaging.ImageAttributes(); colorAreaImageAttr.SetWrapMode(WrapMode.TileFlipXY); e.Graphics.DrawImage(this.srcImage, colorArea, 0, 0, this.srcImage.Width, this.srcImage.Height - 1, GraphicsUnit.Pixel, colorAreaImageAttr); e.Graphics.DrawRectangle(SystemPens.ControlDark, colorBoxOuter); e.Graphics.DrawRectangle(SystemPens.ControlLightLight, colorBoxInner); e.Graphics.DrawLines(this.Enabled ? Pens.Black : SystemPens.ControlDark, new Point[] { new Point(0, pickerVisualPos - this.pickerSize), new Point(this.pickerSize, pickerVisualPos), new Point(0, pickerVisualPos + this.pickerSize), new Point(0, pickerVisualPos - this.pickerSize) }); e.Graphics.DrawLines(this.Enabled ? Pens.Black : SystemPens.ControlDark, new Point[] { new Point(colorBoxOuter.Right + this.pickerSize, pickerVisualPos - this.pickerSize), new Point(colorBoxOuter.Right, pickerVisualPos), new Point(colorBoxOuter.Right + this.pickerSize, pickerVisualPos + this.pickerSize), new Point(colorBoxOuter.Right + this.pickerSize, pickerVisualPos - this.pickerSize) }); if (this.innerPicker) { Pen innerPickerPen = this.valTemp.GetLuminance() > 0.5f ? Pens.Black : Pens.White; e.Graphics.DrawLine(innerPickerPen, new Point(colorArea.Left, pickerVisualPos), new Point(colorArea.Left + 2, pickerVisualPos)); e.Graphics.DrawLine(innerPickerPen, new Point(colorArea.Right - 1, pickerVisualPos), new Point(colorArea.Right - 1 - 2, pickerVisualPos)); } if (!this.Enabled) { e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, SystemColors.Control)), colorArea); } }
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Rectangle colorBox = new Rectangle( this.ClientRectangle.X + this.pickerSize, this.ClientRectangle.Y + this.pickerSize, this.ClientRectangle.Width - this.pickerSize * 2, this.ClientRectangle.Height - this.pickerSize * 2); Rectangle colorArea = this.ColorAreaRectangle; int pickerVisualPos = colorArea.Y + (int)Math.Round((1.0f - this.pickerPos) * colorArea.Height); if (this.min.A < 255 || this.max.A < 255) { e.Graphics.FillRectangle(new HatchBrush(HatchStyle.LargeCheckerBoard, this.renderer.ColorLightBackground, this.renderer.ColorDarkBackground), colorArea); } System.Drawing.Imaging.ImageAttributes colorAreaImageAttr = new System.Drawing.Imaging.ImageAttributes(); colorAreaImageAttr.SetWrapMode(WrapMode.TileFlipXY); e.Graphics.DrawImage(this.srcImage, colorArea, 0, 0, this.srcImage.Width, this.srcImage.Height - 1, GraphicsUnit.Pixel, colorAreaImageAttr); this.renderer.DrawBorder(e.Graphics, colorBox, Drawing.BorderStyle.ContentBox, BorderState.Normal); Pen outerPickerPen = this.Enabled ? Pens.Black : new Pen(Color.FromArgb(128, Color.Black)); e.Graphics.DrawLines(outerPickerPen, new Point[] { new Point(0, pickerVisualPos - this.pickerSize), new Point(this.pickerSize, pickerVisualPos), new Point(0, pickerVisualPos + this.pickerSize), new Point(0, pickerVisualPos - this.pickerSize) }); e.Graphics.DrawLines(outerPickerPen, new Point[] { new Point(colorBox.Right - 1 + this.pickerSize, pickerVisualPos - this.pickerSize), new Point(colorBox.Right - 1, pickerVisualPos), new Point(colorBox.Right - 1 + this.pickerSize, pickerVisualPos + this.pickerSize), new Point(colorBox.Right - 1 + this.pickerSize, pickerVisualPos - this.pickerSize) }); if (this.innerPicker) { Pen innerPickerPen = this.valTemp.GetLuminance() > 0.5f ? Pens.Black : Pens.White; e.Graphics.DrawLine(innerPickerPen, new Point(colorArea.Left, pickerVisualPos), new Point(colorArea.Left + 2, pickerVisualPos)); e.Graphics.DrawLine(innerPickerPen, new Point(colorArea.Right - 1, pickerVisualPos), new Point(colorArea.Right - 1 - 2, pickerVisualPos)); } if (!this.Enabled) { e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, this.renderer.ColorBackground)), colorArea); } }
/// <summary> /// Изменение размера изображения с сохранением пропорций и без потери качества /// </summary> /// <param name="pathToOriginalFile">Абсолютный путь к оригиналу</param> /// <param name="MaxImageSizeToResize">Размер квадрата, в которй вписывать изображение</param> /// <returns></returns> public static System.Drawing.Bitmap Resize(string pathToOriginalFile, int MaxImageSizeToResize) { using (var image = System.Drawing.Image.FromFile(pathToOriginalFile)) { return(LocalGet(image)); } System.Drawing.Bitmap LocalGet(System.Drawing.Image image) { int rW = 0; int rH = 0; double c = 0; if (MaxImageSizeToResize > (double)image.Height) { c = ((double)image.Height / (double)MaxImageSizeToResize); rW = image.Width; rH = image.Height; } else { c = ((double)image.Height / (double)MaxImageSizeToResize); rW = (int)(image.Width / c); rH = MaxImageSizeToResize; } var destRect = new System.Drawing.Rectangle(0, 0, rW, rH); var destImage = new System.Drawing.Bitmap(rW, rH); destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (var graphics = System.Drawing.Graphics.FromImage(destImage)) { graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; using (var wrapMode = new System.Drawing.Imaging.ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel, wrapMode); } } return(destImage); } }
private static Bitmap resizeImage(Bitmap sourceImage, System.Drawing.Size sizeOut) { int sourceWidth = sourceImage.Width; int sourceHeight = sourceImage.Height; float nPercent = 0; float nPercentW = 0; float nPercentH = 0; nPercentW = ((float)sizeOut.Width / (float)sourceWidth); nPercentH = ((float)sizeOut.Height / (float)sourceHeight); if (nPercentH < nPercentW) { nPercent = nPercentH; } else { nPercent = nPercentW; } int destWidth = (int)(sourceWidth * nPercent); int destHeight = (int)(sourceHeight * nPercent); var destRect = new Rectangle(0, 0, destWidth, destHeight); int leftOffset = (int)((sizeOut.Width - destWidth) / 2); int topOffset = (int)((sizeOut.Height - destHeight) / 2); Bitmap destImage = new Bitmap(sizeOut.Width, sizeOut.Height); destImage.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution); using (var graphics = Graphics.FromImage((Image)destImage)) { graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; using (var wrapMode = new System.Drawing.Imaging.ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); graphics.DrawImage(sourceImage, destRect, leftOffset, topOffset, sourceImage.Width, sourceImage.Height, GraphicsUnit.Pixel, wrapMode); } } return(destImage); }
//Scale the origional image to the provides size. Note: The images height and withe are made the same in the constructer by pading the image with a transparent background Image scaleImage(int size) { Rectangle rec = new Rectangle(0, 0, size, size); Bitmap resizedImage = new Bitmap(size, size); resizedImage.SetResolution(inputImage.HorizontalResolution, inputImage.VerticalResolution); using (Graphics graphics = Graphics.FromImage(resizedImage)) { graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; using (System.Drawing.Imaging.ImageAttributes wrapmode = new System.Drawing.Imaging.ImageAttributes()) { wrapmode.SetWrapMode(WrapMode.TileFlipXY); graphics.DrawImage(inputImage, rec, 0, 0, inputImage.Width, inputImage.Height, GraphicsUnit.Pixel, wrapmode); } } return(resizedImage); }
//Image Resize and Image Crop public Bitmap Resize(Image image, int width, int height) { Rectangle kare = new Rectangle(0, 0, width, height); Bitmap orantılanmışresim = new Bitmap(width, height); orantılanmışresim.SetResolution(image.HorizontalResolution, image.VerticalResolution); Graphics g = Graphics.FromImage(orantılanmışresim); g.CompositingMode = CompositingMode.SourceCopy; g.CompositingQuality = CompositingQuality.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; using (var wrapMode = new System.Drawing.Imaging.ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); g.DrawImage(image, kare, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); } return(orantılanmışresim); }
/// <summary> /// Resize the image to the specified width and height. /// </summary> /// <param name="image">The image to resize.</param> /// <param name="width">The width to resize to.</param> /// <param name="height">The height to resize to.</param> /// <returns>The resized image.</returns> public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height) { System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(0, 0, width, height); System.Drawing.Bitmap destImage = new System.Drawing.Bitmap(width, height); destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(destImage)) { graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; using (System.Drawing.Imaging.ImageAttributes wrapMode = new System.Drawing.Imaging.ImageAttributes()) { wrapMode.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY); graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel, wrapMode); } // End Using wrapMode } // End Using graphics return(destImage); }
private void Resize(string srcImageFile, double adjustPercent) { using (Image curImg = Image.FromFile(srcImageFile)) { int newwidth = (int)(curImg.Width * adjustPercent); int newHeight = (int)(curImg.Height * adjustPercent); var destRect = new Rectangle(0, 0, newwidth, newHeight); //float AspectRatio = (float)curImg.Size.Width / (float)curImg.Size.Height; Bitmap newImage = new Bitmap(newwidth, newHeight); newImage.SetResolution(curImg.HorizontalResolution, curImg.VerticalResolution); using (Graphics gr = Graphics.FromImage(newImage)) { gr.CompositingMode = CompositingMode.SourceCopy; gr.CompositingQuality = CompositingQuality.HighQuality; gr.SmoothingMode = SmoothingMode.HighQuality; gr.InterpolationMode = InterpolationMode.HighQualityBicubic; gr.PixelOffsetMode = PixelOffsetMode.HighQuality; //gr.DrawImage(curImg, new Rectangle(0, 0, newwidth, newHeight)); using (var wrapMode = new System.Drawing.Imaging.ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); gr.DrawImage(curImg, destRect, 0, 0, curImg.Width, curImg.Height, GraphicsUnit.Pixel, wrapMode); } } string saveFFN = Path.Combine(Path.GetDirectoryName(srcImageFile), string.Format("{0}_{1}.tiff", Path.GetFileNameWithoutExtension(srcImageFile), adjustPercent.ToString())); if (File.Exists(saveFFN)) { File.Delete(saveFFN); } newImage.Save(saveFFN, System.Drawing.Imaging.ImageFormat.Tiff); } }
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Rectangle colorBoxOuter = new Rectangle( this.ClientRectangle.X + this.pickerSize, this.ClientRectangle.Y + this.pickerSize, this.ClientRectangle.Width - this.pickerSize * 2 - 1, this.ClientRectangle.Height - this.pickerSize * 2 - 1); Rectangle colorBoxInner = new Rectangle( colorBoxOuter.X + 1, colorBoxOuter.Y + 1, colorBoxOuter.Width - 2, colorBoxOuter.Height - 2); Rectangle colorArea = this.ColorAreaRectangle; int pickerVisualPos = colorArea.Y + (int)Math.Round((1.0f - this.pickerPos) * colorArea.Height); if (this.min.A < 255 || this.max.A < 255) e.Graphics.FillRectangle(new HatchBrush(HatchStyle.LargeCheckerBoard, Color.LightGray, Color.Gray), colorArea); System.Drawing.Imaging.ImageAttributes colorAreaImageAttr = new System.Drawing.Imaging.ImageAttributes(); colorAreaImageAttr.SetWrapMode(WrapMode.TileFlipXY); e.Graphics.DrawImage(this.srcImage, colorArea, 0, 0, this.srcImage.Width, this.srcImage.Height - 1, GraphicsUnit.Pixel, colorAreaImageAttr); e.Graphics.DrawRectangle(SystemPens.ControlDark, colorBoxOuter); e.Graphics.DrawRectangle(SystemPens.ControlLightLight, colorBoxInner); e.Graphics.DrawLines(this.Enabled ? Pens.Black : SystemPens.ControlDark, new Point[] { new Point(0, pickerVisualPos - this.pickerSize), new Point(this.pickerSize, pickerVisualPos), new Point(0, pickerVisualPos + this.pickerSize), new Point(0, pickerVisualPos - this.pickerSize)}); e.Graphics.DrawLines(this.Enabled ? Pens.Black : SystemPens.ControlDark, new Point[] { new Point(colorBoxOuter.Right + this.pickerSize, pickerVisualPos - this.pickerSize), new Point(colorBoxOuter.Right, pickerVisualPos), new Point(colorBoxOuter.Right + this.pickerSize, pickerVisualPos + this.pickerSize), new Point(colorBoxOuter.Right + this.pickerSize, pickerVisualPos - this.pickerSize)}); if (this.innerPicker) { Pen innerPickerPen = this.valTemp.GetLuminance() > 0.5f ? Pens.Black : Pens.White; e.Graphics.DrawLine(innerPickerPen, new Point(colorArea.Left, pickerVisualPos), new Point(colorArea.Left + 2, pickerVisualPos)); e.Graphics.DrawLine(innerPickerPen, new Point(colorArea.Right - 1, pickerVisualPos), new Point(colorArea.Right - 1 - 2, pickerVisualPos)); } if (!this.Enabled) { e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, SystemColors.Control)), colorArea); } }
public void UpdateDominoes() { IColorSpaceComparison comp; switch (m_regression_mode) { case 0: comp = new CmcComparison(); break; case 1: comp = new Cie1976Comparison(); break; case 2: comp = new Cie94Comparison(); break; default: comp = new CieDe2000Comparison(); break; } float scaling_x = (SourceImage.Width - 1) / size_x; float scaling_y = (SourceImage.Height - 1) / size_y; if (!m_allow_stretch) { if (scaling_x > scaling_y) { scaling_x = scaling_y; } else { scaling_y = scaling_x; } } // fix transparency: if image is transparent, background appears black Bitmap notransparency = new Bitmap(SourceImage.Width, SourceImage.Height); Graphics temp = Graphics.FromImage(notransparency); temp.Clear(Color.White); System.Drawing.Imaging.ImageAttributes Att = new System.Drawing.Imaging.ImageAttributes(); Att.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY); temp.DrawImage(SourceImage, new Rectangle(0, 0, SourceImage.Width, SourceImage.Height), 0, 0, SourceImage.Width, SourceImage.Height, GraphicsUnit.Pixel, Att); // image to read from WriteableBitmap pixelsource = BitmapFactory.ConvertToPbgra32Format(ImageHelper.BitmapToBitmapSource(notransparency)); pixelsource.Lock(); System.Windows.Media.Color[] sourceColors = new System.Windows.Media.Color[shapes.Length]; dominoes = new int[shapes.Length]; if (!calculation_mode) { // if source pixel = top left pixel of each domino for (int i = 0; i < shapes.Length; i++) { RectangleF container = shapes[i].GetContainer(scaling_x, scaling_y); try { sourceColors[i] = pixelsource.GetPixel((int)container.X, (int)container.Y); } catch (Exception) { } dominoes[i] = Dithering.Compare(new Rgb() { R = sourceColors[i].R, G = sourceColors[i].G, B = sourceColors[i].B }.To <Lab>(), comp, lab_colors, 0); } } else { // if source pixel is average of region for (int i = 0; i < shapes.Length; i++) { RectangleF container = shapes[i].GetContainer(scaling_x, scaling_y); int r = 0, g = 0, b = 0; int counter = 0; // for loop: each container for (float x_iterator = container.X; x_iterator < container.X + container.Width; x_iterator++) { for (float y_iterator = container.Y; y_iterator < container.Y + container.Width; y_iterator++) { if (shapes[i].IsInside(new PointF(x_iterator, y_iterator), true, scaling_x, scaling_y)) { System.Windows.Media.Color c = pixelsource.GetPixel((int)x_iterator, (int)y_iterator); r += c.R; g += c.G; b += c.B; counter++; } } } sourceColors[i] = System.Windows.Media.Color.FromRgb((byte)(r / counter), (byte)(g / counter), (byte)(b / counter)); // calculates the color of the domino dominoes[i] = Dithering.Compare(new Rgb() { R = sourceColors[i].R, G = sourceColors[i].G, B = sourceColors[i].B }.To <Lab>(), comp, lab_colors, 0); } } }
/// <summary> /// Generates a thumbnail /// </summary> /// <param name="tgen">The /// <see cref="Microsoft.Expression.Encoder.ThumbnailGenerator"/> /// to use to generate the thumbnail.</param> /// <param name="time">The time of the thumbnail.</param> /// <param name="thumbWidth">Width of the thumbnail.</param> /// <param name="thumbHeight">Height of the thumbnail.</param> /// <param name="srcRect">The source rect to use when clipping the thumbnail.</param> /// <returns>A <see cref="System.Drawing.Bitmap"/>.</returns> /// <remarks> /// Generates the thumbnail at the orignal source size and then resizes it. /// </remarks> public static System.Drawing.Bitmap GenerateThumbnail(MSEEncoder.ThumbnailGenerator tgen, TimeSpan time, int thumbWidth, int thumbHeight, System.Drawing.Rectangle srcRect) { System.Drawing.Bitmap resized = new System.Drawing.Bitmap (thumbWidth, thumbHeight); System.Drawing.Bitmap original = null; int counter = 0; while (original == null) { try { original = tgen.CreateThumbnail (time); } catch (MSEEncoder.UnableToCreateThumbnailException) { THelper.Error ("Unable to create thumbnail at time {0}", time.ToString (@"h\:mm\:ss\.ffff")); time += new TimeSpan (0, 0, 0, 0, 50); THelper.Error (" Trying time {0}", time.ToString (@"h\:mm\:ss\.ffff")); counter++; if (counter > 20) throw; } } using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage (resized)) { // No alpha channel usage graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; //graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; // Affects image resizing graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; // Affects anti-aliasing of filled edges //graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; using (System.Drawing.Imaging.ImageAttributes att = new System.Drawing.Imaging.ImageAttributes ()) { att.SetWrapMode (System.Drawing.Drawing2D.WrapMode.TileFlipXY); graphics.DrawImage (original, new System.Drawing.Rectangle (0, 0, thumbWidth, thumbHeight), srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height, System.Drawing.GraphicsUnit.Pixel, att); } } original.Dispose(); return resized; }
/// <summary> /// Generates a thumbnail OBSOLETE /// </summary> /// <param name="mediaItem">The <see cref="Microsoft.Expression.Encoder.MediaItem"/> /// to use to generate the thumbnail.</param> /// <param name="time">The time of the thumbnail.</param> /// <param name="thumbWidth">Width of the thumbnail.</param> /// <param name="thumbHeight">Height of the thumbnail.</param> /// <param name="srcRect">The source rect to use when clipping the thumbnail.</param> /// <returns>A <see cref="System.Drawing.Bitmap"/>.</returns> /// <remarks> /// Generates the thumbnail at the orignal source size and then resizes it. /// </remarks> public static System.Drawing.Bitmap GenerateThumbnail(MSEEncoder.MediaItem mediaItem, TimeSpan time, int thumbWidth, int thumbHeight, System.Drawing.Rectangle srcRect) { System.Drawing.Bitmap resized = new System.Drawing.Bitmap (thumbWidth, thumbHeight); using (System.Drawing.Bitmap original = mediaItem.MainMediaFile.GetThumbnail (time, mediaItem.OriginalVideoSize)) { using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage (resized)) { // No alpha channel usage graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; //graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; // Affects image resizing graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; // Affects anti-aliasing of filled edges //graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; using (System.Drawing.Imaging.ImageAttributes att = new System.Drawing.Imaging.ImageAttributes ()) { att.SetWrapMode (System.Drawing.Drawing2D.WrapMode.TileFlipXY); graphics.DrawImage (original, new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight), srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height, System.Drawing.GraphicsUnit.Pixel, att); } } } return resized; }
internal void ExportProcess(DrawArgs drawArgs, RenderableObject.ExportInfo expInfo) { try { bool bChildren = false; if (!isInitialized || textures == null || textures[0] == null) return; if (!drawArgs.WorldCamera.ViewFrustum.Intersects(BoundingBox)) return; if (northWestChild != null && northWestChild.isInitialized) { northWestChild.ExportProcess(drawArgs, expInfo); bChildren = true; } if (northEastChild != null && northEastChild.isInitialized) { northEastChild.ExportProcess(drawArgs, expInfo); bChildren = true; } if (southWestChild != null && southWestChild.isInitialized) { southWestChild.ExportProcess(drawArgs, expInfo); bChildren = true; } if (southEastChild != null && southEastChild.isInitialized) { southEastChild.ExportProcess(drawArgs, expInfo); bChildren = true; } if (!bChildren && this.textures != null && this.textures[0] != null) { Image img = null; try { int iWidth, iHeight, iX, iY; img = Image.FromFile(imageFilePath); iWidth = (int)Math.Ceiling((this.east - this.west) * (double)expInfo.iPixelsX / (expInfo.dMaxLon - expInfo.dMinLon)); iHeight = (int)Math.Ceiling((this.north - this.south) * (double)expInfo.iPixelsY / (expInfo.dMaxLat - expInfo.dMinLat)); iX = (int)Math.Floor((this.west - expInfo.dMinLon) * (double)expInfo.iPixelsX / (expInfo.dMaxLon - expInfo.dMinLon)); iY = (int)Math.Floor((expInfo.dMaxLat - this.north) * (double)expInfo.iPixelsY / (expInfo.dMaxLat - expInfo.dMinLat)); System.Drawing.Imaging.ImageAttributes oAttrs = new System.Drawing.Imaging.ImageAttributes(); oAttrs.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY); // This "eliminates" gaps between images expInfo.gr.DrawImage(img, new Rectangle(iX, iY, iWidth, iHeight), 0.0f, 0.0f, (float)img.Width, (float)img.Height, GraphicsUnit.Pixel, oAttrs); } catch { #if DEBUG System.Diagnostics.Debug.WriteLine("Thrown in image export"); #endif } finally { if (img != null) img.Dispose(); } } } catch { } }
/// <summary> /// Add a <see cref="System.Drawing.Bitmap"/> thumbnail to the page. /// </summary> /// <param name="thumbnail">The <see cref="System.Drawing.Bitmap"/> /// thumbnail.</param> /// <param name="time">The <see cref="TimeSpan">time</see> the thumbnail was /// captured.</param> /// <param name="fileNum">The file number /// (>0 for multi-file <see cref="AVFileSet"/>s).</param> /// <param name="fileStartTime">The file start time.</param> /// <param name="highlight">if set to <c>true</c> highlight thumbnail border.</param> /// <returns> /// <c>true</c> if the page is now full. /// </returns> public bool Add(System.Drawing.Bitmap thumbnail, TimeSpan time, int fileNum, TimeSpan fileStartTime, bool highlight) { int x = _tgrid.Layout.Margin + _tgrid.Layout.Border + _currentCol * (_tgrid.ThumbWidth + _tgrid.Layout.Margin + 2*_tgrid.Layout.Border); int y = _tgrid.Layout.Height + _tgrid.Layout.Border - (_tgrid.NRows - _currentRow) * (_tgrid.ThumbHeight + _tgrid.Layout.Margin + 2*_tgrid.Layout.Border); using (System.Drawing.Imaging.ImageAttributes att = new System.Drawing.Imaging.ImageAttributes ()) { att.SetWrapMode (System.Drawing.Drawing2D.WrapMode.TileFlipXY); System.Drawing.Rectangle thumbRect = new System.Drawing.Rectangle (x, y, _tgrid.ThumbWidth, _tgrid.ThumbHeight); System.Drawing.Rectangle borderRect = thumbRect; _graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; _graphics.DrawImage (thumbnail, thumbRect, 0, 0, _tgrid.ThumbWidth, _tgrid.ThumbHeight, System.Drawing.GraphicsUnit.Pixel, att); thumbRect.Inflate (-_tgrid.Layout.Margin, -_tgrid.Layout.Margin); if (_tgrid.Layout.Border > 0) { borderRect.X -= _tgrid.Layout.Border; borderRect.Y -= _tgrid.Layout.Border; borderRect.Width += _tgrid.Layout.Border; borderRect.Height += _tgrid.Layout.Border; //borderRect.Width = borderRect.Width + 1; //borderRect.Height = borderRect.Height + 1; if (highlight) { _graphics.DrawRectangle (_borderHilightPen, borderRect); borderRect.Inflate (1, 1); _graphics.DrawRectangle (_borderHilightPen, borderRect); } else _graphics.DrawRectangle (_borderPen, borderRect); //borderRect.Offset (-1, -1); //_graphics.DrawRectangle (System.Drawing.Pens.Red, borderRect); //borderRect.Offset (2, 2); //_graphics.DrawRectangle (System.Drawing.Pens.Green, borderRect); } TimeSpan relativeTime = time - fileStartTime; string timeString; string timeFormat = @"h\:mm\:ss"; if (time.Milliseconds > 0) { if (_creator.TNSettings.AlwaysShowMilliseconds || (_pageNum > 0 && fileNum > 0)) timeFormat = @"h\:mm\:ss\.ffff"; else if (time.Milliseconds >= 500) time += _oneSecond; } timeString = time.ToString (timeFormat); // Multi-part files if (fileNum > 0) { //Detail pages if (_pageNum > 0) { timeFormat = @"h\:mm\:ss"; if (relativeTime.Milliseconds > 0) timeFormat = @"h\:mm\:ss\.ffff"; timeString += String.Format (" (#{0} {1})", fileNum, relativeTime.ToString (timeFormat)); } else { if (highlight) { timeString += String.Format (" #{0}", fileNum); } } } System.Drawing.RectangleF thumbRectF = (System.Drawing.RectangleF) thumbRect; _graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver; #if false //_graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit; //_graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; _graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; _graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; _graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; #endif if (_creator.TNSettings.LabelPosition != ThumbnailSettings.LabelPositions.None) { _graphics.DrawString (timeString, _font, _brushBlack, thumbRectF, _thumbFormat); thumbRectF.Offset (-1, -1); _graphics.DrawString (timeString, _font, _brushWhite, thumbRectF, _thumbFormat); } } _currentCol++; if (_currentCol >= _tgrid.NColumns) { _currentCol = 0; _currentRow++; } return _currentRow >= _tgrid.NRows; }
protected override void OnPaint(PaintEventArgs e) { try { using (Bitmap bmp = new Bitmap(this.Width, this.Height)) { using (Graphics g = Graphics.FromImage(bmp)) { //使填充矩形的颜色从红色到黄色渐变 int _height = Math.Max(this.Height - formTitleLeftImg.Height, 1); Rectangle _fillRect = new Rectangle(0, formTitleLeftImg.Height - 1, this.Width, _height); LinearGradientBrush lBrush = new LinearGradientBrush(_fillRect, LinearBackColor1, LinearBackColor2, LinearGradientMode.Vertical | LinearGradientMode.Horizontal); g.FillRectangle(lBrush, _fillRect); // g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighSpeed; g.InterpolationMode = InterpolationMode.NearestNeighbor; System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes(); ia.ClearColorKey(); //绘制角 //左边 g.DrawImage(formDegBottomLeftImg, new Rectangle(0, this.Height - formDegBottomLeftImg.Height, formDegBottomLeftImg.Width, formDegBottomLeftImg.Height), 0, 0, formDegBottomLeftImg.Width, formDegBottomLeftImg.Height, GraphicsUnit.Pixel, ia); //右边 g.DrawImage(formDegBottomRightImg, new Rectangle(this.Width - formDegBottomRightImg.Width, this.Height - formDegBottomRightImg.Height, formDegBottomRightImg.Width, formDegBottomRightImg.Height), 0, 0, formDegBottomRightImg.Width, formDegBottomRightImg.Height, GraphicsUnit.Pixel, ia); //绘制边框 //底边 ia.SetWrapMode(WrapMode.TileFlipX); g.DrawImage(formBorderBottomImg, new Rectangle(formDegBottomLeftImg.Width, this.Height - formBorderBottomImg.Height, this.Width - formDegBottomLeftImg.Width * 2, formBorderBottomImg.Height), 0, 0, formBorderBottomImg.Width, formBorderBottomImg.Height, GraphicsUnit.Pixel, ia); //左边 ia.SetWrapMode(WrapMode.TileFlipY); g.DrawImage(formBorderLeftImg, new Rectangle(0, titleHeight, formBorderLeftImg.Width, this.Height - titleHeight - formDegBottomLeftImg.Height), 0, 0, formBorderLeftImg.Width, formBorderLeftImg.Height, GraphicsUnit.Pixel, ia); //右边 g.DrawImage(formBorderRightImg, new Rectangle(this.Width - formBorderRightImg.Width, titleHeight, formBorderRightImg.Width, this.Height - titleHeight - formDegBottomLeftImg.Height), 0, 0, formBorderRightImg.Width, formBorderRightImg.Height, GraphicsUnit.Pixel, ia); //标题栏 ia.SetWrapMode(WrapMode.TileFlipX); //left g.DrawImage(formTitleLeftImg, new Rectangle(0, 0, formTitleLeftImg.Width, formTitleLeftImg.Height), 0, 0, formTitleLeftImg.Width, formTitleLeftImg.Height, GraphicsUnit.Pixel, ia); //middle g.DrawImage(formTitleMiddleImg, new Rectangle(formTitleLeftImg.Width, 0, this.Width - formTitleLeftImg.Width - formTitleRightImg.Width, formTitleMiddleImg.Height), 0, 0, formTitleMiddleImg.Width, formTitleMiddleImg.Height, GraphicsUnit.Pixel, ia); //right g.DrawImage(formTitleRightImg, new Rectangle(this.Width - formTitleRightImg.Width, 0, formTitleRightImg.Width, formTitleRightImg.Height), 0, 0, formTitleRightImg.Width, formTitleRightImg.Height, GraphicsUnit.Pixel, ia); //绘制标题信息 SizeF s = g.MeasureString(Text, Font); float x = 5; float y = (formTitleLeftImg.Height - s.Height) / 2 + 2; StringFormat sf = new StringFormat(); g.DrawString(Text, new Font(Font.FontFamily, 10), new SolidBrush(Color.FromArgb(230, 230, 230)), x, y, sf); //画到窗口上 e.Graphics.DrawImageUnscaled(bmp, 0, 0, this.Width, this.Height); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
//Paint background if selected private void Form1_Paint(object sender, PaintEventArgs e) { if ((!bTransparency & !bAero) | (bAero & (bAeroTexture|bAeroGradient))) { Rectangle BaseRectangle = new Rectangle(0, 0, this.Width, this.Height); if (bGradient | (bAero&bAeroGradient)) { Color c1 = cColorB1, c2 = cColorB2; if (bAero & bAeroGradient) { c1 = Color.FromArgb((byte)(fAeroTransparency * (float)255),c1); c2 = Color.FromArgb((byte)(fAeroTransparency * (float)255),c2); } Brush Gradient_Brush = new LinearGradientBrush( BaseRectangle, c1, c2, iGradAngle); e.Graphics.FillRectangle(Gradient_Brush, BaseRectangle); } else { Image ImageBack = null; if (imageBackG != null) ImageBack = imageBackG; else ImageBack = Properties.Resources.grey; System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes(); float[][] colorMatrixElements = { new float[] {fRedScale, 0, 0, 0, 0}, // red scaling factor new float[] {0, fGreenScale, 0, 0, 0}, // green scaling factor new float[] {0, 0, fBlueScale, 0, 0}, // blue scaling factor new float[] {0, 0, 0, 1f, 0}, // alpha scaling factor of 1 new float[] {fRed, fGreen, fBlue, 0, 0}}; // three translations System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix( colorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); if(bAero & bAeroTexture) ImageBack = ChangeImageOpacity(ImageBack, fAeroTransparency); int width = ImageBack.Width; int height = ImageBack.Height; if (width > this.Width) { ImageBack.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); ImageBack.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); height = (height * this.Width) / width; width = this.Width; ImageBack = ImageBack.GetThumbnailImage(width, height, null, IntPtr.Zero); } if (bBGImageFill) e.Graphics.DrawImage( ImageBack, BaseRectangle, 0, 0, // upper-left corner of source rectangle width, // width of source rectangle height, // height of source rectangle GraphicsUnit.Pixel, imageAttributes); else { imageAttributes.SetWrapMode(System.Drawing.Drawing2D.WrapMode.Tile); Rectangle brushRect = new Rectangle(0, 0, width, height); TextureBrush tBrush = new TextureBrush(ImageBack, brushRect, imageAttributes); e.Graphics.FillRectangle(tBrush, BaseRectangle); tBrush.Dispose(); } ImageBack = null; //} // e.Graphics.DrawImage(ImageBack, BaseRectangle); } } //else //{ // Image ImageBack = null; // Rectangle BaseRectangle = // new Rectangle(0, 0, this.Width, this.Height); // if (imageBackG != null) // ImageBack = imageBackG; // else // ImageBack = Properties.Resources.grey; // ImageBack = ChangeImageOpacity(ImageBack, fAeroTransparency); // int width = ImageBack.Width; // int height = ImageBack.Height; // if (width > this.Width) // { // ImageBack.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); // ImageBack.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); // height = (height * this.Width) / width; // width = this.Width; // ImageBack = ImageBack.GetThumbnailImage(width, height, null, IntPtr.Zero); // } // System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes(); // if (bBGImageFill) // e.Graphics.DrawImage( // ImageBack, // BaseRectangle, // 0, 0, // upper-left corner of source rectangle // width, // width of source rectangle // height, // height of source rectangle // GraphicsUnit.Pixel, // imageAttributes); // else // { // imageAttributes.SetWrapMode(System.Drawing.Drawing2D.WrapMode.Tile); // Rectangle brushRect = new Rectangle(0, 0, width, height); // TextureBrush tBrush = new TextureBrush(ImageBack, brushRect, imageAttributes); // e.Graphics.FillRectangle(tBrush, BaseRectangle); // tBrush.Dispose(); // } // ImageBack = null; // //} // // e.Graphics.DrawImage(ImageBack, BaseRectangle); //} }
public static Bitmap ResizeImage(string imageFilePath) { Image image = Image.FromFile(imageFilePath); var destRect = new Rectangle(0, 0, width, height); var destImage = new Bitmap(width, height); destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (var graphics = Graphics.FromImage(destImage)) { graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; using (var wrapMode = new System.Drawing.Imaging.ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.Tile); graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); } // Fix orientation if needed. if (image.PropertyIdList.Contains(OrientationKey)) { var orientation = (int)image.GetPropertyItem(OrientationKey).Value[0]; switch (orientation) { case NotSpecified: // Assume it is good. case NormalOrientation: // No rotation required. break; case MirrorHorizontal: destImage.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case UpsideDown: destImage.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case MirrorVertical: destImage.RotateFlip(RotateFlipType.Rotate180FlipX); break; case MirrorHorizontalAndRotateRight: destImage.RotateFlip(RotateFlipType.Rotate90FlipX); break; case RotateLeft: destImage.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case MirorHorizontalAndRotateLeft: destImage.RotateFlip(RotateFlipType.Rotate270FlipX); break; case RotateRight: destImage.RotateFlip(RotateFlipType.Rotate270FlipNone); break; default: throw new NotImplementedException("An orientation of " + orientation + " isn't implemented."); } } } destImage.Save($"{PREFIX}{Path.GetFileName(imageFilePath)}"); return(destImage); }
private void pBox_Paint(object sender, PaintEventArgs e) { lRed.Text = fRed.ToString(); lGreen.Text = fGreen.ToString(); lBlue.Text = fBlue.ToString(); lRedScale.Text = fRedScale.ToString(); lGreenScale.Text = fGreenScale.ToString(); lBlueScale.Text = fBlueScale.ToString(); Rectangle BaseRectangle = new Rectangle(0, 0, pBox.Width, pBox.Height); Image ImageBack = null; if (imageBackG == null) { ImageBack = Properties.Resources.grey; } else { ImageBack = imageBackG; } System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes(); float[][] colorMatrixElements = { new float[] {fRedScale, 0, 0, 0, 0}, // red scaling factor new float[] {0, fGreenScale, 0, 0, 0}, // green scaling factor new float[] {0, 0, fBlueScale, 0, 0}, // blue scaling factor new float[] {0, 0, 0, 1, 0}, // alpha scaling factor of 1 new float[] {fRed, fGreen, fBlue, 0, 1}}; //translations System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix( colorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); int width = ImageBack.Width; int height = ImageBack.Height; // what to do when pic is larger than picture box...? I decided to scale it down to picbox width //if (height > pBox.Height) //{ // ImageBack.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); // ImageBack.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); // width = (width * pBox.Height) / height; // height = pBox.Height; // ImageBack = ImageBack.GetThumbnailImage(width, height, null, IntPtr.Zero); //} if (width > pBox.Width) { ImageBack.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); ImageBack.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); height = (height * pBox.Width) / width; width = pBox.Width; ImageBack = ImageBack.GetThumbnailImage(width, height, null, IntPtr.Zero); } if (bBGIFill) e.Graphics.DrawImage( ImageBack, BaseRectangle, 0, 0, // upper-left corner of source rectangle width, // width of source rectangle height, // height of source rectangle GraphicsUnit.Pixel, imageAttributes); else { imageAttributes.SetWrapMode(System.Drawing.Drawing2D.WrapMode.Tile); Rectangle brushRect = new Rectangle(0, 0, width, height); TextureBrush tBrush = new TextureBrush(ImageBack, brushRect, imageAttributes); e.Graphics.FillRectangle(tBrush, BaseRectangle); tBrush.Dispose(); } // ImageBack.Dispose(); }
private void CaptureScaledScreen(object state) { try { var count = Interlocked.Increment(ref executionCount); var size = new System.Drawing.Size((int)System.Windows.SystemParameters.WorkArea.Width, (int)System.Windows.SystemParameters.WorkArea.Height); using (var screenBmp = new Bitmap(size.Width, size.Height)) { var destRect = new Rectangle(0, 0, this.width, this.height); var destImage = new Bitmap(this.width, this.height); using (Graphics screenGraphics = Graphics.FromImage(screenBmp), destGraphics = Graphics.FromImage(destImage) ) { screenGraphics.CopyFromScreen(new System.Drawing.Point(0, 0), new System.Drawing.Point(0, 0), size); Bitmap32 bm32 = new Bitmap32(screenBmp); bm32.LockBitmap(); Rectangle src_rect = Bitmap32.ImageBounds(bm32); bm32.UnlockBitmap(); // Copy the non-white area. int wid = src_rect.Width; int hgt = src_rect.Height; Bitmap bm = new Bitmap(wid, hgt); using (Graphics gr = Graphics.FromImage(bm)) { gr.Clear(Color.White); Rectangle dest_rect = new Rectangle( 0, 0, src_rect.Width, src_rect.Height); gr.DrawImage(screenBmp, dest_rect, src_rect, GraphicsUnit.Pixel); } destGraphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; destGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bicubic; //destGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; using (var wrapMode = new System.Drawing.Imaging.ImageAttributes()) { wrapMode.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY); destGraphics.DrawImage(bm, destRect, 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel, wrapMode); } //destImage.Save("test.png"); } this.ExtractColors(destImage); } } catch (Exception) { } }
private void config_employee_card_Load(object sender, EventArgs e) { GF.showLoading(this); Dictionary <string, string> values = new() { { "user_id", GF.userID } }; Dictionary <string, object> Obj = DB.Post("Employee/getEmpCardFileName/", values); if (Obj != null) { if (Obj.ContainsKey("result")) { Dictionary <string, object> Item = GF.ToType <Dictionary <string, object> >(Obj["result"]); if (Item.Keys.Count > 0) { if (string.IsNullOrEmpty(Item["filename"]?.ToString())) { this.Height = minHeight; } else { bool isFTPPicture = false; if (File.Exists(GF.Settings("emp_card") + Item["filename"]?.ToString())) { thePicture = Image.FromFile(GF.Settings("emp_card") + Item["filename"]?.ToString()); } else { isFTPPicture = true; thePicture = FTP.download("emp_card", Item["filename"]?.ToString()); } var destRect = new Rectangle(0, 0, 502, 325); var destImage = new Bitmap(502, 325); destImage.SetResolution(thePicture.HorizontalResolution, thePicture.VerticalResolution); using (var graphics = Graphics.FromImage(destImage)) { graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; using var wrapMode = new System.Drawing.Imaging.ImageAttributes(); wrapMode.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY); graphics.DrawImage(thePicture, destRect, 0, 0, thePicture.Width, thePicture.Height, GraphicsUnit.Pixel, wrapMode); } if (isFTPPicture) { var downloadedMessageInfo = new DirectoryInfo(GF.Settings("emp_card")); foreach (FileInfo file in downloadedMessageInfo.GetFiles()) { file.Delete(); } destImage.Save(GF.Settings("emp_card") + Item["filename"]?.ToString(), System.Drawing.Imaging.ImageFormat.Jpeg); } pictureBox.Image = destImage; } } } } else { this.Height = minHeight; GF.closeLoading(); GF.Error("เกิดความผิดพลาดในการรับชื่อไฟล์จาก Server !"); } this.CenterToScreen(); GF.closeLoading(); }
private void drawBitmap() { if (pictureBox1.Image != null) { pictureBox1.Image.Dispose(); pictureBox1.Image = null; } if (!splitContainer1.Panel2.Enabled || map == null) return; // List used to hold all bitmaps to be processed List<bitmapData> bitmaps = new List<bitmapData>(); #region Add backgrounds & menu items to list & sort into depth rendering order #region add lbBitmaps to list (background images) if (lbBitmaps.Items.Count > 1) { for (int i = 1; i < lbBitmaps.Items.Count; i++) { bitmapData bd = (bitmapData)lbBitmaps.Items[i]; int step = 0; while (step < bitmaps.Count && bd.renderDepth > bitmaps[step].renderDepth) step++; bitmaps.Insert(step, bd); } } #endregion paneData pd = (paneData)lbPanes.SelectedItem; #region add List Blocks to list (Menu Items & backdrops) if (pd.listBlocks.Count > 0) for (int i = 0; i < pd.listBlocks.Count; i++) { List<bitmapData> bds = (List<bitmapData>)pd.listBlocks[i].link; foreach (bitmapData bd in bds) { int step = 0; // renderDepth >= ( as opposed to just > ) so text takes upper priority while (step < bitmaps.Count && bd.renderDepth >= bitmaps[step].renderDepth) step++; bitmaps.Insert(step, bd); } } #endregion #region add Text Blocks to list (Information Text) if (pd.textBlocks.Count > 0) for (int i = 0; i < pd.textBlocks.Count; i++) { List<bitmapData> bds = (List<bitmapData>)pd.textBlocks[i].link; foreach (bitmapData bd in bds) { int step = 0; // renderDepth >= ( as opposed to just > ) so text takes upper priority while (step < bitmaps.Count && bd.renderDepth >= bitmaps[step].renderDepth) step++; bitmaps.Insert(step, bd); } } #endregion #endregion // create a bitmap to hold the combined image Bitmap finalImage = new Bitmap(pictureBox1.Width, pictureBox1.Height); // (1024, 512) (602, 480) ??? // get a graphics object from the image so we can draw on it using (Graphics g = Graphics.FromImage(finalImage)) { #region Background bitmap drawing // Some menus have no background bitmaps if (bitmaps.Count > 0) { // set background color g.Clear(Color.Empty); // go through each image and draw it on the final image #region LoadBitmap(Meta meta); foreach (bitmapData bd in bitmaps) { if (bd.meta == null) continue; Bitmap b = (Bitmap)bd.link; if (b == null) continue; int xOff = (int)(-bd.horizontalWrapsPerSec * tickTimer) % b.Width; int yOff = (int)(bd.verticalWrapsPerSec * tickTimer) % b.Height; xOff = xOff >= 0 ? xOff : xOff + b.Width; yOff = yOff >= 0 ? yOff : yOff + b.Height; System.Drawing.Imaging.ImageAttributes imgAttr = new System.Drawing.Imaging.ImageAttributes(); imgAttr.SetWrapMode(System.Drawing.Drawing2D.WrapMode.Tile); g.DrawImage( b, new Rectangle( finalImage.Width / 2 + bd.left, finalImage.Height / 2 - bd.top, b.Width, b.Height), xOff, yOff, b.Width, b.Height, GraphicsUnit.Pixel, imgAttr ); } #endregion } #endregion #region Bitmap outlining if (tabControl1.SelectedTab == tpBitmaps) { if (lbBitmaps.SelectedIndex > 0) { bitmapData bd = (bitmapData)lbBitmaps.SelectedItem; if (bd.link != null) { int origLeft = finalImage.Width / 2 + bd.left; int left = origLeft > 0 ? origLeft : 1; int origTop = finalImage.Height / 2 - bd.top; int top = origTop > 0 ? origTop : 1; int width = ((Bitmap)bd.link).Width - (left - origLeft); int height = ((Bitmap)bd.link).Height - (top - origTop); g.DrawRectangle( new Pen(Color.Red, 2.0f), new Rectangle( left, top, (width + left) < finalImage.Width ? width : finalImage.Width - left - 1, (height + top) < finalImage.Height ? height : finalImage.Height - top - 1 )); } } } #endregion #region List Box lines if (tabControl1.SelectedTab == tpListBlocks) { if (((paneData)lbPanes.SelectedItem).listBlocks.Count > 0) { listBlockData ld = (listBlockData)((paneData)lbPanes.SelectedItem).listBlocks[0]; int startX = finalImage.Width / 2; int startY = finalImage.Height / 2; g.DrawLines( new Pen(Color.Red, 2.0f), new Point[] { new Point(startX + ld.left, startY - ld.bottom - 100), new Point(startX + ld.left, startY - ld.bottom), new Point(startX + ld.left + 100, startY - ld.bottom) }); g.DrawLines( new Pen(Color.Red, 1.5f), new Point[] { new Point(startX + ld.left - 4, startY - ld.bottom - 40), new Point(startX + ld.left - 4, startY - ld.bottom + 4), new Point(startX + ld.left + 40, startY - ld.bottom + 4) }); } } #endregion #region Text Block outlining if (tabControl1.SelectedTab == tpTextBlocks) { if (lbTBTextBlocks.SelectedIndex >= 0) { textBlockData td = (textBlockData)lbTBTextBlocks.SelectedItem; int origLeft = finalImage.Width / 2 + td.left - 1; int left = origLeft > 0 ? origLeft : 1; int origTop = finalImage.Height / 2 - td.top - 1; int top = origTop > 0 ? origTop : 1; int width = finalImage.Width / 2 + td.right - (left) + 2; int height = finalImage.Height / 2 - td.bottom - (top - origTop) + 2; g.DrawRectangle( new Pen(Color.Red, 2.0f), new Rectangle( left, top, (width + left) < finalImage.Width ? width : finalImage.Width - left - 1, (height + top) < finalImage.Height ? height : finalImage.Height - top - 1 )); } } #endregion g.Dispose(); } pictureBox1.Image = finalImage; // Perform garbage collection or memory usage can quickly raise to > 1Gb until collection is performed GC.Collect(); }