Hi,
Please check the below code,
AnnContainer container;
//AnnAutomationManager manager;
RasterImageViewer viewer;
RasterCodecs codecs;
AnnDesigner currentDesigner;
public Form1(string title)
{
Text = title;
Size = new Size(500, 200);
viewer = new RasterImageViewer();
viewer.TransformChanged += new EventHandler(viewer_TransformChanged);
viewer.PostImagePaint += new PaintEventHandler(viewer_PostImagePaint);
viewer.MouseDown += new MouseEventHandler(viewer_MouseDown);
viewer.MouseMove += new MouseEventHandler(viewer_MouseMove);
viewer.MouseUp += new MouseEventHandler(viewer_MouseUp);
viewer.LostFocus += new EventHandler(viewer_LostFocus);
// load an image into the viewer
RasterCodecs.Startup();
codecs = new RasterCodecs();
viewer.Image = new RasterImage(Image.FromFile("c:\\PE5.jpg"));
RasterCodecs.Shutdown();
// create and set up the container
container = new AnnContainer();
container.Bounds = new AnnRectangle(0, 0, viewer.ImageSize.Width, viewer.ImageSize.Height);
container.UnitConverter = new AnnUnitConverter(viewer.ImageDpiX, viewer.ImageDpiY);
// Create a panel with three buttons
Panel panel = new Panel();
panel.Dock = DockStyle.Left;
Controls.Add(panel);
panel.BringToFront();
// Create three buttons
Button buttonLine = new Button();
buttonLine.Click += new EventHandler(buttonLine_Click);
buttonLine.Text = "Line";
buttonLine.Dock = DockStyle.Top;
panel.Controls.Add(buttonLine);
buttonLine.BringToFront();
Button buttonInsertText = new Button();
buttonInsertText.Click += new EventHandler(buttonInsertText_Click);
buttonInsertText.Text = "Insert Text";
buttonInsertText.Dock = DockStyle.Bottom;
panel.Controls.Add(buttonInsertText);
buttonInsertText.BringToFront();
Button buttonSelect = new Button();
buttonSelect.Click += new EventHandler(buttonSelect_Click);
buttonSelect.Text = "Select";
buttonSelect.Dock = DockStyle.Left;
panel.Controls.Add(buttonSelect);
buttonSelect.BringToFront();
// Add the viewer
viewer.Dock = DockStyle.Fill;
Controls.Add(viewer);
viewer.BringToFront();
}
private void viewer_TransformChanged(object sender, EventArgs e)
{
// set up the container transformation
if (viewer.Image != null && container != null)
container.Transform = viewer.Transform.Clone();
}
private void viewer_PostImagePaint(object sender, PaintEventArgs e)
{
// draw the container and its objects on this viewer
if (viewer.Image != null && container != null)
container.Draw(e.Graphics);
}
private void buttonSelect_Click(object sender, System.EventArgs e)
{
// Select button is clicked
// cancel any draw designer running
if (currentDesigner != null && currentDesigner is AnnDrawDesigner)
{
AnnDrawDesigner drawDesigner = currentDesigner as AnnDrawDesigner;
drawDesigner.Cancel();
currentDesigner = null;
}
}
private void buttonLine_Click(object sender, System.EventArgs e)
{
// Line button is clicked
// first end any current designer (if any)
EndDesigner();
// start a new Line draw designer
AnnLineDrawDesigner lineDrawDesigner = new AnnLineDrawDesigner();
// set up the object template (a 2 pixels-wide pen)
AnnLineObject lineObject = new AnnLineObject();
lineObject.Pen = new AnnPen(Color.Red, new AnnLength(2, AnnUnit.Pixel));
lineDrawDesigner.ObjectTemplate = lineObject;
StartDrawDesigner(lineDrawDesigner);
}
private void buttonInsertText_Click(object sender, System.EventArgs e)
{
EndDesigner();
AnnTextDrawDesigner textDrawDesigner = new AnnTextDrawDesigner();
// AnnRectangleDrawDesigner rectangleDrawDesigner = new AnnRectangleDrawDesigner();
AnnTextObject text = new AnnTextObject();
text.Text = "Sandy";
text.Bounds = new AnnRectangle(50, 50, 100, 200, AnnUnit.Pixel);
text.Pen = new AnnPen(Color.Red, new AnnLength(2, AnnUnit.Pixel));
text.Brush = new AnnSolidBrush(Color.Yellow);
textDrawDesigner.ObjectTemplate = text;
StartDrawDesigner(textDrawDesigner);
}
private void EndDesigner()
{
// ends any running designer
if (currentDesigner != null)
{
if (currentDesigner is AnnEditDesigner)
{
AnnEditDesigner editDesigner = currentDesigner as AnnEditDesigner;
editDesigner.End();
}
else if (currentDesigner is AnnDrawDesigner)
{
AnnDrawDesigner drawDesigner = currentDesigner as AnnDrawDesigner;
drawDesigner.Cancel();
}
}
}
private void StartDrawDesigner(AnnDrawDesigner drawDesigner)
{
// set up the current designer
drawDesigner.Draw += new EventHandler(OnDesignerDraw);
drawDesigner.Owner = viewer;
drawDesigner.ClipCursor = true;
drawDesigner.Container = container;
currentDesigner = drawDesigner;
}
private void OnDesignerDraw(object sender, AnnDrawDesignerEventArgs e)
{
// show information on the current draw operation
Console.Write("Status: {0}, Object: {1}, Coordinates:", e.OperationStatus, e.Object.GetType().Name);
if (e.Object is AnnLineObject)
{
AnnLineObject lineObject = e.Object as AnnLineObject;
Console.WriteLine("Start Point: {0}, EndPoint: {1}", lineObject.StartPoint, lineObject.EndPoint);
}
else if (e.Object is AnnTextObject)
{
AnnTextObject textObject = e.Object as AnnTextObject;
Console.WriteLine("Bounds: {0}", textObject.Bounds);
}
}
private void viewer_MouseDown(object sender, MouseEventArgs e)
{
bool handled = false;
// see if a designer is currently running, if so, let it handle this event
if (currentDesigner != null)
handled = currentDesigner.MouseDown(e);
if (!handled)
{
// the mouse click was ! handled by a designer
// check if the click was on top of an existing object that we can start editing
AnnPoint pt = new AnnPoint(e.X, e.Y);
AnnObject obj = container.HitTest(pt, 2);
if (obj != null)
{
// yes, start the edit designer for this object
if (obj is AnnLineObject)
{
AnnLineEditDesigner lineEditDesigner = new AnnLineEditDesigner();
StartEditing(lineEditDesigner, obj, pt);
lineEditDesigner.MouseDown(e);
}
else if (obj is AnnTextObject)
{
AnnRectangleEditDesigner rectangleEditDesigner = new AnnRectangleEditDesigner();
StartEditing(rectangleEditDesigner, obj, pt);
rectangleEditDesigner.MouseDown(e);
}
}
}
}
private void StartEditing(AnnEditDesigner editDesigner, AnnObject obj, AnnPoint pt)
{
// first end any running designers
EndDesigner();
// set up the current designer
editDesigner.Edit += new EventHandler(OnDesignerEdit);
editDesigner.Owner = viewer;
editDesigner.Container = container;
editDesigner.ClipCursor = true;
editDesigner.EditObject = obj;
editDesigner.HitTestBuffer = 2;
editDesigner.ControlPointsHitTestBuffer = 2;
editDesigner.RotateModifierKey = Keys.Shift;
editDesigner.HitTestObject = obj.HitTest(pt, 2);
// set up the edit designer control points
AnnRectangleControlPoint rectangleControlPoint = new AnnRectangleControlPoint();
rectangleControlPoint.Size = new AnnSize(10, 10, AnnUnit.Pixel);
rectangleControlPoint.Pen = new AnnPen(Color.Blue, new AnnLength(1, AnnUnit.Pixel));
rectangleControlPoint.Brush = new AnnSolidBrush(Color.White);
for (int i = 0; i < editDesigner.ControlPointCount; i++)
editDesigner.ControlPoints[i] = rectangleControlPoint;
editDesigner.Start();
currentDesigner = editDesigner;
}
private void OnDesignerEdit(object sender, AnnEditDesignerEventArgs e)
{
// show information on the current draw operation
Console.Write("Object: {0}, Operation: {1}, Status: {2}, ", e.Object.GetType().Name, e.Operation, e.OperationStatus);
if (e.Operation == AnnEditDesignerOperation.MoveControlPoint)
Console.WriteLine("Control Point index: {0}", e.MoveControlPointIndex);
else
Console.WriteLine("HitTestObject: {0}", e.HitTestObject);
}
private void viewer_MouseMove(object sender, MouseEventArgs e)
{
bool handled = false;
// see if a designer is currently running, if so, let it handle this event
if (currentDesigner != null)
handled = currentDesigner.MouseMove(e);
}
private void viewer_MouseUp(object sender, MouseEventArgs e)
{
bool handled = false;
// see if a designer is currently running, if so, let it handle this event
if (currentDesigner != null)
handled = currentDesigner.MouseUp(e);
}
private void viewer_LostFocus(object sender, EventArgs e)
{
// see if a designer is currently running, if so, cancel it
if (currentDesigner != null)
currentDesigner.Cancel();
}
public void AnnDesigner_AnnDesigner(string title)
{
Form1 form = new Form1(title);
form.ShowDialog();
}
private void Form1_Load(object sender, EventArgs e)
{
AnnDesigner_AnnDesigner("san");
}
when you click on Insert Text it will draw only rectangle on image the text will not get displayed in that rectangle so please tell me what changes I will do in this so that text in rectangle will be visible and how i will get the location and height and weidth of that rectangle.
Also please tell me how should I provide rotate points to rotate that rectangle.Please replay soon.
Thanks
Manju