C#で画像の拡大縮小の際にクリックしたところへの拡大の処理についてです。
単に拡大するとクリック(Ctl + 右クリックで拡大、Ctl + 左クリックで縮小)した場所ではないところが拡大されます。
それをクリックした場所で拡大縮小縮小できるようにしたいと思っています。
pointをつかってx,y座標を指定してあげればいいと思ったのですが、拡大されたとき座標はもとの画像と異なるためうまくいきません。
拡大は4倍のため、拡大後の座標をもとのx,y座標x,yの4倍にしてやればいいと思ったのですがこれもとおりません。
なにかクリックしたポイントへの拡大縮小をおこなう方法はあるでしょうか?
ちなみにツールバーで動かせるようになっているので、ツールバーによる座標の取得という方法をとっています。
ソースコードを提示してくれないと回答のしようもありません
質問頂きありがとうございます
ソースコードが長いようであればgoogle driveのリンクやgithubでも構いません
みなさま、ご返信ありがとうございます。
ソースコード追記いたします。
//表示する画像
private Bitmap currentImage;
//倍率
private double zoomRatio = 1d;
//倍率変更後の画像のサイズと位置
private Rectangle drawRectangle;
//Button1のクリックイベントハンドラ
private void Button1_Click(object sender, EventArgs e)
{
//表示する画像を読み込む
if (currentImage != null)
{
currentImage.Dispose();
}
currentImage = new Bitmap(TextBox1.Text);
//初期化
drawRectangle = new Rectangle(0, 0, currentImage.Width, currentImage.Height);
zoomRatio = 1d;
//画像を表示する
PictureBox1.Invalidate();
}
//PictureBox1のMouseDownイベントハンドラ
private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
{
PictureBox pb = (PictureBox)sender;
//クリックされた位置を画像上の位置に変換
Point imgPoint = new Point(
(int)Math.Round((e.X - drawRectangle.X) / zoomRatio),
(int)Math.Round((e.Y - drawRectangle.Y) / zoomRatio));
//倍率を変更する
if (e.Button == MouseButtons.Left)
{
zoomRatio *= 2d;
}
else if (e.Button == MouseButtons.Right)
{
zoomRatio *= 0.5d;
}
//倍率変更後の画像のサイズと位置を計算する
drawRectangle.Width = (int)Math.Round(currentImage.Width * zoomRatio);
drawRectangle.Height = (int)Math.Round(currentImage.Height * zoomRatio);
drawRectangle.X = (int)Math.Round(pb.Width / 2d - imgPoint.X * zoomRatio);
drawRectangle.Y = (int)Math.Round(pb.Height / 2d - imgPoint.Y * zoomRatio);
//画像を表示する
PictureBox1.Invalidate();
}
//PictureBox1のPaintイベントハンドラ
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
if (currentImage != null)
{
//画像をクリックした位置で拡大描画する(拡大画像が400%のため)
g.DrawImage(bmp, 4*x, 4*y, resizeWidth, resizeHeight);
}
}
よろしくお願いします。

あなたの回答
tips
プレビュー