问题在PictureBox中缩放图像 [英] Problem Zooming Image in PictureBox

查看:108
本文介绍了问题在PictureBox中缩放图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图片框上的图像恢复到原始大小,并且缓冲区在前面被破坏了
图片框的....
请帮帮我...
这是代码...

The image on picturebox go back to the original size and buffer gone broken infront
of the picturebox....
help me please...
this is the code...

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

推荐答案

我要假设这是WinForms,它看起来肯定是这样,但我不确定.以下是您要实现的代码:
http://www.bobpowell.net/zoompicbox.htm [
Im going to assume this is WinForms, it sure looks like that, but im not certain. Here is code for what you are trying to achieve:
http://www.bobpowell.net/zoompicbox.htm[^]


这篇关于问题在PictureBox中缩放图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆