2012-11-05 213 views
2

我在放大圖像時遇到問題。我已成功縮放圖像,但圖像在向上或向下滾動時會中斷。C縮放圖像#

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

} 
+0

當您嘗試滾動時圖像會發生什麼變化?它顯示不正確嗎? – 3aw5TZetdf

+0

圖像分成兩個部分,每個部分覆蓋,頂部的圖像是縮放的結果,底部的圖像是初始大小的圖像。當在圖片頂部滾動緩慢擦除。 – AdeSusan

回答

2

你應該吸取Paint事件圖像(例如):

pictureBox.Invalidate(); 

和你的代碼應該是這樣的:

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

和調整通話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(); 
} 
+0

謝謝....你的回答非常非常有幫助..... – AdeSusan

+0

你好,歡迎;) – Ria