void UpdateImage() { ClearImage(); pictureBox1.Image = _bitmap.ToGdiBitmap(); _selection = new RectF(1f, 1f); UpdateImageScale(); }
/// <summary> /// Round up the image /// </summary> /// <param name="sourceBitmap"></param> /// <returns></returns> public static C1Bitmap RoundUpImage(C1Bitmap sourceBitmap) { var image = sourceBitmap.ToGdiBitmap(); var width = image.Width; var height = image.Height; using (var transformBitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb)) { using (var graphics = Graphics.FromImage(transformBitmap)) { graphics.Clear(Color.Transparent); var path = new GraphicsPath(FillMode.Alternate); int radius = 15; int diameter = radius * 2; path.StartFigure(); path.AddLine(radius, 0, width - radius, 0); path.AddArc(new RectangleF(width - diameter, 0, diameter, diameter), -90, 90); path.AddLine(width, radius, width, height - radius); path.AddArc(new RectangleF(width - diameter, height - diameter, diameter, diameter), 0, 90); path.AddLine(width - radius, height, radius, height); path.AddArc(new RectangleF(0, height - diameter, diameter, diameter), 90, 90); path.AddLine(0, height - radius, 0, radius); path.AddArc(new RectangleF(0, 0, diameter, diameter), 180, 90); path.CloseFigure(); graphics.SetClip(path); graphics.DrawImage(image, Point.Empty); } sourceBitmap.Import(transformBitmap); return(sourceBitmap); } }
public void Rotate(int delta) { var rotateOption = (TransformOptions)Enum.ToObject(typeof(TransformOptions), delta); var transform = new FlipRotator(rotateOption); Bitmap = Bitmap.Transform(transform); Image = Bitmap.ToGdiBitmap(); }
// C1Bitmap holder. public Picture(Bitmap source) { Bitmap = new C1Bitmap(); if (source != null) { using (MemoryStream stream = new MemoryStream()) { source.Save(stream, System.Drawing.Imaging.ImageFormat.Png); Bitmap.Load(stream); } } SizeMode = PictureBoxSizeMode.StretchImage; Image = Bitmap.ToGdiBitmap(); Width = 120; Height = 120; }
void UpdateImage() { if (_bitmap == null || !_bitmap.HasImage) { return; } var bmp = pictureBox1.Image as Bitmap; if (bmp != null) { bmp.Dispose(); } bmp = _bitmap.ToGdiBitmap(); pictureBox1.Image = bmp; pictureBox1.Width = bmp.Width; pictureBox1.Height = bmp.Height; _selection = new RectF(1f, 1f); }
void UpdateImageSource(ImageEffect imageEffect) { // some effects can change pixels outside the bounds of the source // image, so we need a margin to make those pixels visible var targetOffset = new Point2F(_marginLT, _marginLT); int w = _bitmap.PixelWidth + _marginLT + _marginRB; int h = _bitmap.PixelHeight + _marginLT + _marginRB; // the render target object var rt = _d2dContext; // create the target Direct2D bitmap var bpTarget = new D2D.BitmapProperties1( new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied), (float)_bitmap.DpiX, (float)_bitmap.DpiY, D2D.BitmapOptions.Target | D2D.BitmapOptions.CannotDraw); var targetBmp = D2D.Bitmap1.Create(rt, new Size2L(w, h), bpTarget); // associate the target bitmap with render target rt.SetTarget(targetBmp); // start drawing rt.BeginDraw(); // clear the target bitmap rt.Clear(null); // convert C1Bitmap image to Direct2D image var d2dBitmap = _bitmap.ToD2DBitmap1(rt, D2D.BitmapOptions.None); // apply the effect or just draw the original image switch (imageEffect) { case ImageEffect.Original: rt.DrawImage(d2dBitmap, targetOffset); break; case ImageEffect.GaussianBlur: rt.DrawImage(ApplyGaussianBlur(d2dBitmap), targetOffset); break; case ImageEffect.Sharpen: rt.DrawImage(ApplySharpen(d2dBitmap), targetOffset); break; case ImageEffect.HorizontalSmear: rt.DrawImage(ApplyHorizontalSmear(d2dBitmap), targetOffset); break; case ImageEffect.Shadow: rt.DrawImage(ApplyShadow(d2dBitmap), targetOffset); break; case ImageEffect.DisplacementMap: rt.DrawImage(ApplyDisplacementMap(d2dBitmap), targetOffset); break; case ImageEffect.Emboss: rt.DrawImage(ApplyEmboss(d2dBitmap), targetOffset); break; case ImageEffect.EdgeDetect: rt.DrawImage(ApplyEdgeDetect(d2dBitmap), targetOffset); break; case ImageEffect.Sepia: rt.DrawImage(ApplySepia(d2dBitmap), targetOffset); break; } d2dBitmap.Dispose(); // draw the text label in case of the Shadow effect if (imageEffect == ImageEffect.Shadow) { var mr = Matrix3x2.Rotation(-90f); var mt = Matrix3x2.Translation(targetOffset.X + 6f, targetOffset.Y + 344f); rt.Transform = mr * mt; _brush.SetColor(ColorF.White); rt.DrawTextLayout(new Point2F(-1f, -1f), _textLayout, _brush); _brush.SetColor(ColorF.DimGray); rt.DrawTextLayout(Point2F.Empty, _textLayout, _brush); rt.Transform = Matrix3x2.Identity; } // finish drawing (all drawing commands are executed at that moment) if (!rt.EndDraw(true)) { targetBmp.Dispose(); // try to recreate the device resources if the old GPU device was removed DiscardDeviceResources(); CreateDeviceResources(); return; } // detach the target bitmap rt.SetTarget(null); // create a temporary C1Bitmap object var outBitmap = new C1Bitmap(_bitmap.ImagingFactory); // import the image from Direct2D target bitmap to C1Bitmap outBitmap.Import(targetBmp, rt, new RectL(w, h)); targetBmp.Dispose(); // convert C1Bitmap to a System.Drawing.Bitmap ClearGdiBitmap(); _lastGdiBitmap = outBitmap.ToGdiBitmap(); outBitmap.Dispose(); // show the result in the PictureBox pictureBox1.Image = _lastGdiBitmap; }