Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用C语言缩放图像#_C#_Winforms_Image Processing - Fatal编程技术网

C# 用C语言缩放图像#

C# 用C语言缩放图像#,c#,winforms,image-processing,C#,Winforms,Image Processing,我在缩放图像时遇到问题。我已成功缩放图像,但图像在上下滚动时会中断 public partial class ImageDetail : UserControl { public delegate void onClose(); public event onClose close; Image pbImage; public ImageDetail(Image img) { InitializeComponent(

我在缩放图像时遇到问题。我已成功缩放图像,但图像在上下滚动时会中断

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;
    }

}
公共部分类ImageDetail:UserControl
{
公共委托void onClose();
公共事件关闭;
图像;
公共图像详细信息(图像img)
{            
初始化组件();
pictureBox.Image=img;
pbImage=img;
pictureBox.Width=this.pbImage.Width;
pictureBox.Height=this.pbImage.Height;
panel2.AutoScroll=true;
panel2.HorizontalScroll.Maximum=最小宽度;
面板2.VerticalScroll.Maximum=最小高度;
panel2.HorizontalScroll.Minimum=0;
panel2.VerticalScroll.Minimum=0;
面板2.设置自动滚动边距(10,10);
}
私有无效标签2_单击(对象发送方,事件参数e)
{
如果(关闭!=null)
close();
}        
私有void DrawImage(int startX、int startY)
{            
如果(this.pbImage==null){return;}
Graphics pbGraphics=this.pictureBox.CreateGraphics();
BufferedGraphicsContext currentGraphicsContext=BufferedGraphicsManager.Current;
矩形targetRect=新矩形(startX,startY,(this.pbImage.Width+tmpWidth),(this.pbImage.Height+tmpHeight));
使用(BufferedGraphics pbGDIBuffer=currentGraphicsContext.Allocate(pbGraphics,targetRect))
{
矩形drawRect=新矩形(startX,startY,(this.pbImage.Width+tmpWidth),(this.pbImage.Height+tmpHeight));
pbGDIBuffer.Graphics.DrawImageUnscaledClipped(this.pbImage,drawRect);
pbGDIBuffer.Render();
}
pictureBox.Width=this.pbImage.Width+tmpWidth;
pictureBox.Height=this.pbImage.Height+tmpHeight;
}             
int tmpWidth=0,tmpHeight=0;
私有无效工具StripSplitButton1_按钮单击(对象发送方,事件参数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);
}
私有无效工具StripSplitButton2\u按钮单击(对象发送方,事件参数e)
{
如果(tmpWidth>0)
tmpWidth=tmpWidth-((this.pbImage.Width*20)/100);
如果(tmpHeight>0)
tmpHeight=tmpHeight-((this.pbImage.Height*20)/100);
如果(tmpHeight<0)
tmpHeight=0;
如果(tmpWidth<0)
tmpWidth=0;
pictureBox.Refresh();
DrawImage(0,0);
}
专用无效面板2_鼠标向下(对象发送器,鼠标目标e)
{
pictureBox.Width=this.pbImage.Width+tmpWidth;
pictureBox.Height=this.pbImage.Height+tmpHeight;
}
专用无效面板2_鼠标(对象发送器,鼠标目标e)
{
pictureBox.Width=this.pbImage.Width+tmpWidth;
pictureBox.Height=this.pbImage.Height+tmpHeight;
}
}

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

调整调用方法的大小后:


您的代码应该是这样的:

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();
}

当您尝试滚动时,图像会发生什么变化?是否显示不正确?图像分为两个部分,每个部分都覆盖,顶部的图像是缩放的结果,底部的图像是具有初始大小的图像。当在图片上方滚动时慢慢擦除。
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();
}