c# - C# 中的缩放图像

标签 c# winforms image-processing

我在缩放图像时遇到问题。我已成功缩放图像,但向上或向下滚动时图像会破裂。

public partial class ImageDetail : UserControl
{
    public delegate void onClose();
    public event onClose close;
    Image pbImage;
    public ImageDetail(Image img)
    {            
        InitializeComponent();
        pictureBox.Image = img;
        pbImage = img;
        pictureBox.Width = this.pbImage.Width;
        pictureBox.Height = this.pbImage.Height;
        panel2.AutoScroll = true;
        panel2.HorizontalScroll.Maximum = img.Width;
        panel2.VerticalScroll.Maximum   = img.Height;
        panel2.HorizontalScroll.Minimum = 0;
        panel2.VerticalScroll.Minimum   = 0;
        panel2.SetAutoScrollMargin(10, 10);            
    }

    private void label2_Click(object sender, EventArgs e)
    {
        if (close != null)
            close(); 
    }        

    private void DrawImage(int startX, int startY)
    {            
        if (this.pbImage == null) { return; }

        Graphics pbGraphics = this.pictureBox.CreateGraphics();
        BufferedGraphicsContext currentGraphicsContext = BufferedGraphicsManager.Current;
        Rectangle targetRect = new Rectangle(startX, startY, (this.pbImage.Width + tmpWidth), (this.pbImage.Height + tmpHeight));
        using (BufferedGraphics pbGDIBuffer = currentGraphicsContext.Allocate(pbGraphics, targetRect))
        {
            Rectangle drawRect = new Rectangle(startX, startY, (this.pbImage.Width + tmpWidth), (this.pbImage.Height + tmpHeight));

            pbGDIBuffer.Graphics.DrawImageUnscaledAndClipped(this.pbImage, drawRect);
            pbGDIBuffer.Render();                
        }
        pictureBox.Width = this.pbImage.Width + tmpWidth;
        pictureBox.Height = this.pbImage.Height + tmpHeight;
    }             

    int tmpWidth = 0, tmpHeight = 0;
    private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
    {
        tmpWidth = tmpWidth + ((this.pbImage.Width * 20) / 100);
        tmpHeight = tmpHeight + ((this.pbImage.Height * 20) / 100);

        pictureBox.Width = this.pbImage.Width + tmpWidth;
        pictureBox.Height = this.pbImage.Height + tmpHeight;
        pictureBox.Refresh();
        DrawImage(0, 0);
    }

    private void toolStripSplitButton2_ButtonClick(object sender, EventArgs e)
    {
        if(tmpWidth > 0)
            tmpWidth = tmpWidth - ((this.pbImage.Width * 20) / 100);
        if(tmpHeight > 0)
            tmpHeight = tmpHeight - ((this.pbImage.Height * 20) / 100);
        if (tmpHeight < 0)
            tmpHeight = 0;
        if (tmpWidth < 0)
            tmpWidth = 0;
        pictureBox.Refresh();
        DrawImage(0, 0);                      
    }

    private void panel2_MouseDown(object sender, MouseEventArgs e)
    {
        pictureBox.Width = this.pbImage.Width + tmpWidth;
        pictureBox.Height = this.pbImage.Height + tmpHeight;
    }

    private void panel2_MouseUp(object sender, MouseEventArgs e)
    {
        pictureBox.Width = this.pbImage.Width + tmpWidth;
        pictureBox.Height = this.pbImage.Height + tmpHeight;
    }

}

最佳答案

您应该在 Paint 上绘制图像事件(例如):

pictureBox.Paint += (sender, e) =>
    {
        var drawRect = new Rectangle(startX, startY, pictureBox.Width, pictureBox.Height);
        e.Graphics.DrawImageUnscaledAndClipped(this.pbImage, drawRect);
    };

调整大小后调用 Invalidate方法:

pictureBox.Invalidate();

你的代码应该是这样的:

public ImageDetail(Image img)
{
    pictureBox.Image = img;
    pbImage = img;
    ...
    pictureBox.Paint += pictureBox_Paint;
}

void pictureBox_Paint(object sender, PaintEventArgs e)
{
    if (pbImage == null) { return; }
    var drawRect = new Rectangle(startX, startY, pictureBox.Width, pictureBox.Height);
    e.Graphics.DrawImageUnscaledAndClipped(this.pbImage, drawRect);
}

private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
{
    ///... resize to what you need 
    pictureBox.Width = (int) (pbImage.Width*0.2);
    pictureBox.Height = (int) (pbImage.Height * 0.2);
    pictureBox.Invalidate();
}

关于c# - C# 中的缩放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13225419/

相关文章:

c# - 比较 MbUnit 中相同的两个对象

c# - 使用 Group by 的 Linq to SQL

c# - MS Chart X 轴标签重复 - 显示单个项目的多个点 [Range Bar]

c# - 我如何计算使用 DateTime.Now 调用该方法的速度?

c# - 如何与 Parallel.Invoke 并行启动方法?

C# WebKit 和 CKEditor iframe 抓取

image-processing - 使用ImageMagick自动调整大小叠加两个图像

c# - 如何在不打断单词的情况下在 C# 中打断一个长字符串

python - OpenCV 大图像(大于 1gb)替代方案

python - 图像识别中的预处理