이것은 pictureBox1의 페인트 이벤트 코드입니다. 변수 mImage의 위치를 찾아야합니다.이미지 위치를 얻으려면 어떻게해야합니까?
if (null != mImage)
{
e.Graphics.DrawImage(mImage, theLocationOfImage);
}
mImage는 이미지 유형입니다. 대신 LocationIfImage를 사용하여 mImage 위치를 지정해야합니다. 이 내가 mImage을 가지고하는 방법입니다
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
Bitmap bmp = new Bitmap(pictureBox1.Image);
Bitmap bmp1 = GetPartOfImageInRect(bmp, mRect);
CalculateNewSizeFactor(e.Delta);
Image img1 = ResizeImage(bmp1,
new Size((int)(bmp1.Width * currentfactor),
(int)(bmp1.Height * currentfactor)));
mImage = img1;
pictureBox1.Invalidate();
}
mRect 내가 pictureBox1 이상 그릴 사각형이다.
편집
이 난 사각형 그리는 방법은 다음과 같습니다
private void DrawRectangle(Graphics e)
{
using (Pen pen = new Pen(Color.Red, 2))
{
e.DrawRectangle(pen, mRect);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
mRect = new Rectangle(e.X, e.Y, 0, 0);
pictureBox1.Invalidate();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mRect = new Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top);
pictureBox1.Invalidate();
}
}
이 크기 조정 이미지 방법 :
private Image ResizeImage(Image img, Size size)
{
return new Bitmap(img, size);
}
가 그리고 이것은 pictureBox1 페인트 이벤트 :
if (null != mImage)
{
e.Graphics.DrawImage(mImage, theLocationOfImage);
}
DrawRectangle(e.Graphics);
을
그리고 계산 새로운 크기 인자 방법 지속될 :
private void CalculateNewSizeFactor(int delta)
{
if (delta > 0 && factor < 2.5)
{
factor *= increment;
currentfactor = factor;
}
else if (delta < 0 && factor > 0.25)
{
factor /= increment;
currentfactor = factor;
}
}
나는 전체 이미지에서 줌의 크기를 조정할 수 있습니다하지만 난 사각형을 통해 그려 아웃 만의 영역을 확대하고자합니다.
EDIT이 수단을 추가 분실 한
private Bitmap GetPartOfImageInRect(Bitmap source, Rectangle rect)
{
return source.Clone(rect, source.PixelFormat);
}
위치를 지정해야합니다. 뭐하고 싶어? 이미지의 일부분의 크기를 조정하고 원본 이미지의 가운데에 배치 하시겠습니까? 설명 해주십시오! –
올리비에 그래, 나는 pictureBox1에 사각형을 그린다. mRect 변수가 맞다. 마우스 휠을 사용할 때, 확대/축소와 같이 이미지의 재배치 부분 만 변경하면된다. – user3200169
올리비에가 내 질문을 업데이트했다. 필요한 방법과 이벤트. – user3200169