gpt4 book ai didi

c# - 在 C# 中绘制到新的 "layer"

转载 作者:行者123 更新时间:2023-11-30 14:14:10 29 4
gpt4 key购买 nike

构建一个小绘图程序并尝试合并图层的概念。

我正在使用 PictureBox 控件来显示图像,并从 PictureBox 显示的图像中获取 Graphics 对象并绘制到该对象上。

我的问题是我正在尝试弄清楚如何绘制到覆盖在图片框顶部的新 Graphics 对象,并能够在没有原始图像的情况下获取新绘制的图像吸收到图形中的图像。

如果我这样做:

Graphics gr = Graphics.FromImage(myPictureBox.image);
gr.DrawRectangle(blah blah)

...我正在编辑图片框中的原始图像。我想要一种方法来仅捕获作为单独图像绘制的新内容,但仍将其显示为覆盖在已有内容之上。

谁能给我指出正确的方向?谢谢!

最佳答案

我会考虑使用透明控件并做一些修改,以便它可以用作图像层:

http://www.codeproject.com/Articles/26878/Making-Transparent-Controls-No-Flickering

可能是这样的(根据需要进行任何修改)。

class LayerControl : UserControl
{
private Image image;
private Graphics graphics;

public LayerControl(int width, int height)
{
this.Width = width;
this.Height = height;

image = new Bitmap(width, height);

graphics = Graphics.FromImage(image);

// Set style for control
SetStyle(ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint, true);
}

// this function will draw your image
protected override void OnPaint(PaintEventArgs e)
{
var bitMap = new Bitmap(image);
// by default the background color for bitmap is white
// you can modify this to follow your image background
// or create a new Property so it can dynamically assigned
bitMap.MakeTransparent(Color.White);

image = bitMap;

Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.GammaCorrected;

float[][] mtxItens = {
new float[] {1,0,0,0,0},
new float[] {0,1,0,0,0},
new float[] {0,0,1,0,0},
new float[] {0,0,0,1,0},
new float[] {0,0,0,0,1}};

ColorMatrix colorMatrix = new ColorMatrix(mtxItens);

ImageAttributes imgAtb = new ImageAttributes();
imgAtb.SetColorMatrix(
colorMatrix,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);

g.DrawImage(image,
ClientRectangle,
0.0f,
0.0f,
image.Width,
image.Height,
GraphicsUnit.Pixel,
imgAtb);
}

// this function will grab the background image to the control it self
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
Graphics g = e.Graphics;

if (Parent != null)
{
BackColor = Color.Transparent;
int index = Parent.Controls.GetChildIndex(this);

for (int i = Parent.Controls.Count - 1; i > index; i--)
{
Control c = Parent.Controls[i];
if (c.Bounds.IntersectsWith(Bounds) && c.Visible)
{
Bitmap bmp = new Bitmap(c.Width, c.Height, g);
c.DrawToBitmap(bmp, c.ClientRectangle);

g.TranslateTransform(c.Left - Left, c.Top - Top);
g.DrawImageUnscaled(bmp, Point.Empty);
g.TranslateTransform(Left - c.Left, Top - c.Top);
bmp.Dispose();
}
}
}
else
{
g.Clear(Parent.BackColor);
g.FillRectangle(new SolidBrush(Color.FromArgb(255, Color.Transparent)), this.ClientRectangle);
}
}

// simple drawing circle function
public void DrawCircles()
{
using (Brush b = new SolidBrush(Color.Red))
{
using (Pen p = new Pen(Color.Green, 3))
{
this.graphics.DrawEllipse(p, 25, 25, 20, 20);
}
}
}

// simple drawing rectable function
public void DrawRectangle()
{
using (Brush b = new SolidBrush(Color.Red))
{
using (Pen p = new Pen(Color.Red, 3))
{
this.graphics.DrawRectangle(p, 50, 50, 40, 40);
}
}
}

// Layer control image property
public Image Image
{
get
{
return image;
}
set
{
image = value;
// this will make the control to be redrawn
this.Invalidate();
}
}
}

使用示例:

LayerControl lc = new LayerControl(100, 100);
lc.Location = new Point(0, 0);
lc.DrawRectangle();

LayerControl lc2 = new LayerControl(100, 100);
lc2.Location = new Point(0, 0);
lc2.DrawCircles();

LayerControl lc3 = new LayerControl(100, 100);
lc3.Location = new Point(0, 0);
lc3.Image = new Bitmap(@"<Image Path>");

// adding control
this.Controls.Add(dc);
this.Controls.Add(dc2);
this.Controls.Add(dc3);

使用这种方法,您可以拥有多个可以相互重叠的图层(由于它具有透明功能)。

如果您想将它添加到 PictureBox 的顶部,请确保对控件重新排序。图层控件应添加在 PictureBox 控件之前。

// adding control
this.Controls.Clear();
this.Controls.Add(dc);
this.Controls.Add(dc2);
this.Controls.Add(dc3);
this.Controls.Add(PictureBox1);

希望对您有所帮助。

关于c# - 在 C# 中绘制到新的 "layer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12702672/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com