gpt4 book ai didi

c# - C#图像处理非常慢

转载 作者:太空宇宙 更新时间:2023-11-03 17:42:51 25 4
gpt4 key购买 nike

在过去的几个月中,我一直在研究应用程序,它的功能之一就是可以裁剪图像。因此,我编写了一个函数,该函数绘制一个透明的橙色矩形,以向用户显示裁剪区域,但是它的工作速度非常慢-有人可以帮助我/向我展示一种使其更快的方法吗?

这是代码:

Image source;

private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
mousePos = e.Location;
}

Point mousePos;

private void pictureBox1_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
Image editSource = new Bitmap(source);
Graphics g = Graphics.FromImage(editSource);
SolidBrush brush = new SolidBrush(
Color.FromArgb(128, Color.Orange.R, Color.Orange.G, Color.Orange.B));

int width = e.X - mousePos.X;
if (width < 0) {
width *= -1;
}

int height = e.Y - mousePos.Y;
if (height < 0) {
height *= -1;
}

Size cropRectSize = new Size(width, height);
Rectangle cropRect = new Rectangle(mousePos, cropRectSize);
g.FillRectangle(brush, cropRect);
pictureBox1.Image = editSource;
}
}

最佳答案

因此,所有关于不使用图片框的建议...我会给你一种方法来做;)

其背后的想法是仅使用鼠标移动,鼠标向下移动等来存储应绘制的内容。然后在绘制时使用存储的状态。每当您在图片框上按下鼠标时,就会绘制一个橙色矩形(即使建议不要使用图片框,也可以将这种方法用于其他表面。)。

    Point startPoint;
Point currentPoint;
bool draw = false;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
startPoint = e.Location;
draw = true;
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
draw = false;
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
currentPoint = e.Location;
pictureBox1.Invalidate();
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (draw)
{
int startX = Math.Min(startPoint.X, currentPoint.X);
int startY = Math.Min(startPoint.Y, currentPoint.Y);
int endX = Math.Max(startPoint.X, currentPoint.X);
int endY = Math.Max(startPoint.Y, currentPoint.Y);
e.Graphics.DrawRectangle(Pens.Orange, new Rectangle(startX, startY, endX-startX, endY-startY));
}
}

关于c# - C#图像处理非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14714297/

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