トップ回答者
BitmapをForm上でスクロールさせたいのですが

質問
回答
-
acslearner さん、こんにちは
ダッチです。
Paint イベントでの e.Graphics.DrawImage(bmp, 0, 0); この部分ですが、座標 (0, 0) は見えているフォームの左上が (0, 0) のため、毎回同じ画像が表示されています。スクロールしたらスクロールした分だけずらしてやれば、期待通りの動作になると思います。
スクロールしている値を取得するには AutoScrollPosition プロパティを使用します。このプロパティにスクロールした分だけの値が入っていますので描画する座標から AutoScrollPosition プロパティの値を引いてやればいいと思います。AutoScrollPosition プロパティの値はマイナスで入っているので足すといった方がいいかもしれません。
AutoScrollPosition プロパティ
すべての返信
-
少し長いですが、自由に線を描画するソフトです。
線を書いて、スクロールアウトして、Timerで再描画すると、
先ほど書いた線が、現われてしまうのです。
Code Snippetusing System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace DrawFreeLine
{
/// <summary>
/// Form1 の概要の説明です。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private IContainer components;public Form1()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
}/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}#region Windows Form Designer generated code
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 5000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.AutoScroll = true;
this.AutoScrollMinSize = new System.Drawing.Size(1000, 1000);
this.ClientSize = new System.Drawing.Size(292, 272);
this.Name = "Form1";
this.Text = "DrawFreeLine";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);}
#endregion/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}Bitmap bmp = new Bitmap(5000,5000);
int[,] field = new int[1000, 1000];Pen p = new Pen (Color.Red, 3);
Point pnt = new Point();
private Timer timer1;
bool mouseDown = false;
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.DrawImage(bmp, 0, 0);
}private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
pnt.X = e.X;
pnt.Y = e.Y;
mouseDown = true;
}private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (mouseDown==true)
{
Graphics g = Graphics.FromImage(bmp);
g.DrawLine(p, pnt, new Point(e.X,e.Y));
g.Dispose();
pnt.X = e.X;
pnt.Y = e.Y;
Invalidate();
}
}private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
mouseDown = false;
}private void Form1_Load(object sender, EventArgs e)
{
this.DoubleBuffered = true;}
private void timer1_Tick(object sender, EventArgs e)
{
Invalidate();
}
} -
acslearner さん、こんにちは
ダッチです。
Paint イベントでの e.Graphics.DrawImage(bmp, 0, 0); この部分ですが、座標 (0, 0) は見えているフォームの左上が (0, 0) のため、毎回同じ画像が表示されています。スクロールしたらスクロールした分だけずらしてやれば、期待通りの動作になると思います。
スクロールしている値を取得するには AutoScrollPosition プロパティを使用します。このプロパティにスクロールした分だけの値が入っていますので描画する座標から AutoScrollPosition プロパティの値を引いてやればいいと思います。AutoScrollPosition プロパティの値はマイナスで入っているので足すといった方がいいかもしれません。
AutoScrollPosition プロパティ