2008/11/03(Mon) 18:44:01 編集(投稿者)
■No27307 (倉田 有大 さん) に返信
倉田さん、返信ありがとうございます。
倉田さんのご助言の通り、自分のソースコードを修正して、
3つのRectangleを好きな場所に配置することには成功しました。
(※今回の投稿文の最後にそのコードを記載します)
あと知りたいのは、画像拡大機能です。
(ただし、画像拡大時にはRectangleも一緒に拡大してほしい)
【拡大の仕方としては、任意のRectangleの
センターを中心に前後左右100ドット近辺を拡大するです。】
このやりかたまで理解できたら、
自分の作ろうとしているツールが作り上げることができそうですので、
どうか、この点までご教授をお願いできませんでしょうか?
よろしくお願いします。
猫
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace sample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//表示する画像
private Bitmap img;
//画像を表示させる範囲
private Rectangle imgRect;
//表示させるレクタングルの表示範囲
private Rectangle[] rectangle;
//画像描画ボタン
private void ImagePaintButton_Click(object sender, EventArgs e)
{
img = new Bitmap(@"c:\test.jpg");
float ratioW = (float)pictureBox1.Width / img.Width;
float ratioH = (float)pictureBox1.Height / img.Height;
int nw = Convert.ToInt32(img.Width * ratioW);
int nh = Convert.ToInt32(img.Height * ratioH);
imgRect = new Rectangle(0, 0, nw, nh);
pictureBox1.Invalidate();
pictureBox1.Paint += new PaintEventHandler(PicPaint);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (img != null)
{
e.Graphics.DrawImage(img, imgRect);
}
}
void PicPaint(object o, PaintEventArgs e)
{
rectangle = new Rectangle[3];
rectangle[0].X = pictureBox1.Width - 150;
rectangle[0].Y = pictureBox1.Height - 150;
rectangle[1].X = pictureBox1.Width;
rectangle[1].Y = pictureBox1.Height;
rectangle[2].X = pictureBox1.Width + 150;
rectangle[2].Y = pictureBox1.Height + 150;
for (int i = 0; i < 3; i++)
{
rectangle[i].Width = 50;
rectangle[i].Height = 50;
rectangle[i].X = (rectangle[i].X - rectangle[i].Width) / 2;
rectangle[i].Y = (rectangle[i].Y - rectangle[i].Height) / 2;
e.Graphics.DrawRectangle(Pens.Red, rectangle[i]);
}
}
//画像拡大ボタン
private void ExpandImageBotton_Click(object sender, EventArgs e)
{
//任意のRectangle(例えば上で3つ描画しているRectangleのうち、Rectangle[0])の
//センターを中心に前後左右100ドット近辺を拡大する機能を実装したいです。
//どうすればよいでしょうか?
}
}
}