sudheer kumar

sudheer kumar

  • NA
  • 2
  • 1.7k

How to draw accurate shapes in Windows 8 store application

Dec 18 2014 6:15 AM
Hi all,
I am using drawing paint in my windows 8 store application.But the shapes are not getting in a accurate manner. Here is my code below. Kindly help me. Thanks in advance.
public sealed partial class ConjunctivaMainView : UserControl
{
Rect _windowBounds;
InkManager _inkManager = new InkManager();
InkDrawingAttributes _drawingAttributes = new InkDrawingAttributes();
private uint _penID;
private uint _touchID;
Rect selectedStrokes;
private Point _previousContactPt;
private PointerPoint _firstContactPt;
private bool _isMoved;
Color _chosenColor;
Point rememberPt;
int i = 0;
double imgBrWidth;
double imgBrHeigth;
Thickness ViewMargin;
bool IsStrokesLoaded = false;
public ConjunctivaMainView()
{
this.InitializeComponent();
// FlipView4.ItemsSource = PageCollection;
try
{
//ContextControl.ItemsSource = PageCollection;
//ContextControl.SelectedIndex = 0;
_windowBounds = Window.Current.Bounds;
InkCanvas.PointerExited += new PointerEventHandler(InkCanvas_PointerReleased);
ColorsListBox.SelectedIndex = 0;
sizeCombo.SelectedIndex = 1;
_drawingAttributes.PenTip = PenTipShape.Circle;
//ReadFromXML_Click_1();
//ViewMargin = MRBorder.Margin;
//this.ConjunctivaPopupRect.Visibility = Visibility.Collapsed;
//MinimiseViewBoxButton.Visibility = Visibility.Collapsed;
}
catch { }
}
#region Image
private void InkCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
{
try
{
_isMoved = false;
PointerPoint pt = e.GetCurrentPoint(InkCanvas);
_previousContactPt = pt.Position;
_firstContactPt = pt;
if (i == 0)
rememberPt = pt.Position;
i = 1;
PointerDeviceType pointerDevType = e.Pointer.PointerDeviceType;
if (pointerDevType == PointerDeviceType.Pen || pointerDevType == PointerDeviceType.Touch ||
pointerDevType == PointerDeviceType.Mouse &&
pt.Properties.IsLeftButtonPressed)
{
_inkManager.ProcessPointerDown(pt);
_penID = pt.PointerId;
e.Handled = true;
}
}
catch
{
e.Handled = true;
}
}
private void InkCanvas_PointerMoved(object sender, PointerRoutedEventArgs e)
{
try
{
if (e.Pointer.PointerId == _penID)
{
PointerPoint pt = e.GetCurrentPoint(InkCanvas);
Point currentContactPt = pt.Position;
if (Distance(currentContactPt, _previousContactPt) > 2)
{
_isMoved = true;
Line line = new Line()
{
X1 = _previousContactPt.X,
Y1 = _previousContactPt.Y,
X2 = currentContactPt.X,
Y2 = currentContactPt.Y,
StrokeThickness = _drawingAttributes.Size.Width,
Stroke = new SolidColorBrush(_drawingAttributes.Color)
};
_previousContactPt = currentContactPt;
InkCanvas.Children.Add(line);
_inkManager.ProcessPointerUpdate(pt);
}
}
e.Handled = true;
}
catch
{
e.Handled = true;
}
}
private double Distance(Point currentContactPt, Point _previousContactPt)
{
double d = 0;
d = Math.Sqrt(Math.Pow((currentContactPt.X - _previousContactPt.X), 2) + Math.Pow((currentContactPt.Y - _previousContactPt.Y), 2));
return d;
}
private void InkCanvas_PointerReleased(object sender, PointerRoutedEventArgs e)
{
try
{
int strokeCount = -1;
if (e.Pointer.PointerId == _penID)
{
PointerPoint pt = e.GetCurrentPoint(InkCanvas);
_inkManager.ProcessPointerUp(pt);
strokeCount = _inkManager.GetStrokes().Count;
if (strokeCount > 0)
(_inkManager.GetStrokes())[strokeCount - 1].DrawingAttributes = _drawingAttributes;
if (_inkManager.Mode == InkManipulationMode.Selecting)
selectedStrokes = _inkManager.SelectWithLine(_firstContactPt.Position, pt.Position);
}
_touchID = 0;
_penID = 0;
if (_isMoved == true)
{
RenderAllStrokes();
}
e.Handled = true;
}
catch
{
e.Handled = true;
}
}
private async void RenderAllStrokes()
{
var img = InkCanvas.Children.ElementAt(0);
InkCanvas.Children.Clear();
InkCanvas.Children.Add(img);
RenderingHelpers.RenderInk(InkCanvas, _inkManager, _chosenColor);
Task savestrokesTask = SaveStrokes();
await savestrokesTask;
}
private void Eraser_Click(object sender, RoutedEventArgs e)
{
_inkManager.Mode = InkManipulationMode.Erasing;
}
private void Pencil_Click(object sender, RoutedEventArgs e)
{
_inkManager.Mode = InkManipulationMode.Inking;
}
private void ColorsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int selectedIndex = ((ListBox)sender).SelectedIndex;
switch (selectedIndex)
{
case 0:
_drawingAttributes.Color = Colors.Red;
break;
case 1:
_drawingAttributes.Color = Colors.Green;
break;
case 2:
_drawingAttributes.Color = Colors.Blue;
break;
case 3:
_drawingAttributes.Color = Colors.Gray;
break;
case 4:
_drawingAttributes.Color = Colors.Yellow;
break;
case 5:
_drawingAttributes.Color = Colors.Black;
break;
default:
_drawingAttributes.Color = Colors.Black;
break;
}
}
private void sizeCombo_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
int selectedIndex = ((ComboBox)sender).SelectedIndex;
switch (selectedIndex)
{
case 0:
_drawingAttributes.Size = new Size(1.0, 1.0);
break;
case 1:
_drawingAttributes.Size = new Size(3.0, 3.0);
break;
case 2:
_drawingAttributes.Size = new Size(5.0, 5.0);
break;
case 3:
_drawingAttributes.Size = new Size(8.0, 8.0);
break;
default:
_drawingAttributes.Size = new Size(1.0, 1.0);
break;
}
}
byte[] strokes;
InMemoryRandomAccessStream mem;
//private async void Save_Click(object sender, RoutedEventArgs e)
//{
// Task t=SaveStrokes();
// await t;
//}
private async Task SaveStrokes()
{
try
{
// Bug fix - Last stroke wasn't cleared
// by Usha 18th Jun '13
if (_inkManager.GetStrokes().Count > 0)
{
using (InMemoryRandomAccessStream mem = new InMemoryRandomAccessStream())
{
await _inkManager.SaveAsync(mem);
mem.Seek(0);
strokes = new byte[mem.Size];
mem.AsStream().Read(strokes, 0, strokes.Count());
(this.DataContext as EyeTouch.ViewModel.Exams.SLE.ExamConjunctivaViewModel).EyeImageXAML = new byte[strokes.Length];
strokes.CopyTo((this.DataContext as EyeTouch.ViewModel.Exams.SLE.ExamConjunctivaViewModel).EyeImageXAML, 0);
}
}
else
{
strokes = new byte[0];
(this.DataContext as EyeTouch.ViewModel.Exams.SLE.ExamConjunctivaViewModel).EyeImageXAML = new byte[0];
}
}
catch
{
}
}
private async void ReadFromXML_Click_1()
{
try
{
using (InMemoryRandomAccessStream mem = new InMemoryRandomAccessStream())
{
strokes = new byte[(this.DataContext as EyeTouch.ViewModel.Exams.SLE.ExamConjunctivaViewModel).EyeImageXAML.Length];
(this.DataContext as EyeTouch.ViewModel.Exams.SLE.ExamConjunctivaViewModel).EyeImageXAML.CopyTo(strokes, 0);
await mem.AsStream().WriteAsync(strokes, 0, strokes.Length);
mem.AsStream().Flush();
//mem.AsStream().Write(strokes, 0, strokes.Length);
mem.Seek(0);
var ran = mem.GetInputStreamAt(0);
await _inkManager.LoadAsync(ran);
var img = InkCanvas.Children.ElementAt(0);
InkCanvas.Children.Clear();
InkCanvas.Children.Add(img);
RenderingHelpers.RenderInk(InkCanvas, _inkManager, _chosenColor);
}
}
catch (Exception ex)
{
}
}
#endregion

Answers (1)