2010-11-24 14 views
2

私の質問はスタックオーバーフローの質問Draw lines on a picturebox using mouse clicks in C#に関連していますが、マウスボタンが上になると描画された線が消えます。これをどうやって解決するのですか?ピクチャボックスに線を描画する

private void GainBox_MouseDn(object sender, MouseEventArgs e) 
{ 
    mouse_dn = true; 
} 

private void GainBox_MouseMv(object sender, MouseEventArgs e) 
{ 
    //Line drawn from lookup table 
    if (mouse_dn) 
    { 
     img = new Bitmap(256, 256); 

     //Get the coordinates (x, y) for line from lookup table. 

     for (x = x1; x < x2; x++) 
      img.SetPixel(x, y, Color.Red); 

     //Same for y coordinate 
    } 
    GainBox.Refresh(); 
} 

private void GainBox_MouseUp(object sender, MouseEventArgs e) 
{ 
    mouse_dn = false; 
} 
+0

質問タイトルに使用されている言語についての情報が含まれていないと意味がない限り、その情報は含めないでください。タグはこの目的に役立ちます。 –

答えて

0

gainbox.refresh()if (mouse_dn)句内で滞在する必要があります。

0

グラフィックオブジェクトを使用して引き出し線を使用する

Graphics gfx = GainBox.CreateGraphics(); 
gfx.Drawline([Your Parameters here]); 
1

ここには、ポイントに基づいて線を描く小さな完全なプログラムがあります(この場合はマウスに追従します)。私はあなたが必要とするものにそれを再加工できると思います。あなた溶液中

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 


    // Variable that will hold the point from which to draw the next line 
    Point latestPoint; 


    private void GainBox_MouseDown(object sender, MouseEventArgs e) 
    { 
     if ((e.Button & MouseButtons.Left) == MouseButtons.Left) 
     { 
      // Remember the location where the button was pressed 
      latestPoint = e.Location; 
     } 
    } 

    private void GainBox_MouseMove(object sender, MouseEventArgs e) 
    { 
     if ((e.Button & MouseButtons.Left) == MouseButtons.Left) 
     { 
      using (Graphics g = GainBox.CreateGraphics()) 
      { 
       // Draw next line and... 
       g.DrawLine(Pens.Red, latestPoint, e.Location); 

       // ... Remember the location 
       latestPoint = e.Location; 
      } 
     } 
    } 
} 

一つの問題は、あなたが一時的なビットマップに描画されているということですが、そのビットマップの画像は、お使いのPictureBoxに転送されることはありません。ここに示すソリューションでは、余分なビットマップは必要ありません。

+0

@Fedrick:あなたは自由な手描きについて話しています...実際には私は既存の曲線の形を変えたいと思っています....画像上のどこかをクリックすると曲線がその点を通過するはずです。 )画像の入出力曲線です。 –

+0

@Sisya:私の答えの中で重要なことは、ポイントの取得方法ではなく、描画の仕方です。 –

+0

@Fedrick:ええと。実際には、Photoshopの[画像 - >調整 - >曲線]のように、画像ボックス全体に線をドラッグ(描画しない)したい。 –