I am working on zooming selected rectangle of an image. But for some reason the selected area has not been displayed in the picture box. I do want only zoom the selected area. But the following code does not work properly. What could be the problem? Thanks for any help.
private void Form1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (popupMenuZoomOptionSelected)
{
isDrag = true;
Control control = (Control)sender;
ptLast.X = -1;
ptLast.Y = -1;
ptOriginal.X = -1;
ptOriginal.Y = -1;
// Make a note that we "have the mouse".
bHaveMouse = true;
ptOriginal.X = e.X;
ptOriginal.Y = e.Y;
ptLast.X = -1;
ptLast.Y = -1;
startPoint = control.PointToScreen(new Point(e.X, e.Y));
}
}
}
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (isDrag)
{
Point endPoint = ((Control)sender).PointToScreen(new Point(e.X, e.Y));
int width = endPoint.X - startPoint.X;
int height = endPoint.Y - startPoint.Y;
if (bHaveMouse)
{
if (endPoint.X > this.pbImage.Width)
{
endPoint.X = this.pbImage.Width;
}
if (endPoint.X < 0)
{
endPoint.X = 0;
}
if (endPoint.Y < 0)
{
endPoint.Y = 0;
}
if (endPoint.Y > this.pbImage.Height)
{
endPoint.Y = this.pbImage.Height;
}
// Update last point.
ptLast = endPoint;
ControlPaint.DrawReversibleFrame(theRectangle,
this.BackColor, FrameStyle.Dashed);
}
theRectangle = new Rectangle(startPoint.X,
startPoint.Y, width, height);
// Draw the new rectangle by calling DrawReversibleFrame
// again.
ControlPaint.DrawReversibleFrame(theRectangle,
this.BackColor, FrameStyle.Dashed);
}
}
private void Form1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
// If the MouseUp event occurs, the user is not dragging.
isDrag = false;
// Draw the rectangle to be evaluated. Set a dashed frame style
// using the FrameStyle enumeration.
ControlPaint.DrawReversibleFrame(theRectangle,
this.BackColor, FrameStyle.Dashed);
bHaveMouse = false;
Rectangle controlRectangle;
for (int i = 0; i < Controls.Count; i++)
{
controlRectangle = Controls[i].RectangleToScreen
(Controls[i].ClientRectangle);
if (controlRectangle.IntersectsWith(theRectangle))
{
Controls[i].BackColor = Color.BurlyWood;
}
}
if (ptLast.X > ptOriginal.X)
{
selectedAreaWidth = ptLast.X - ptOriginal.X;
selectedAreaStartingPointX = ptOriginal.X;
}
else
{
selectedAreaWidth = ptOriginal.X - ptLast.X;
selectedAreaStartingPointX = ptLast.X;
};
if (ptLast.Y > ptOriginal.Y)
{
selectedAreaStartingPointY = ptOriginal.Y;
selectedAreaHeight = ptLast.Y - ptOriginal.Y;
}
else
{
selectedAreaStartingPointY = ptLast.Y;
selectedAreaHeight = ptOriginal.Y - ptLast.Y;
}
// Set flags to know that there is no "previous" line to reverse.
ptLast.X = -1;
ptLast.Y = -1;
ptOriginal.X = -1;
ptOriginal.Y = -1;
int _ZoomFactor = 2;
int zoomWidth = pbImage.Width / _ZoomFactor;
int zoomHeight = pbImage.Height / _ZoomFactor;
int halfWidth = zoomWidth / 2;
int halfHeight = zoomHeight / 2;
// Create a new temporary bitmap to fit inside the picZoom picturebox
Bitmap tempBitmap = new Bitmap(zoomWidth, zoomHeight, PixelFormat.Format24bppRgb);
Graphics bmGraphics = Graphics.FromImage(tempBitmap);
bmGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
bmGraphics.DrawImage(pbImage.Image,
new Rectangle(0, 0, zoomWidth, zoomHeight),
new Rectangle(e.X - halfWidth, e.Y - halfHeight, zoomWidth, zoomHeight),
GraphicsUnit.Pixel);
// Draw the bitmap on the picZoom picturebox
pbImage.Image = tempBitmap;
ptLast.X = -1;
ptLast.Y = -1;
// Reset the rectangle.
theRectangle = new Rectangle(0, 0, 0, 0);
}