windows phone 多触控画图并保存到 手机图片库

http://code.msdn.microsoft.com/CSWP7MultiTouchDrawing-4513213a 原文

效果图

image

算法分析:

按下鼠标的时候 把这个点加入点的集合,当鼠标移动的时候,把加入的点集 画线

无论是移动鼠标还是按下鼠标都会触发

  Touch.FrameReported += new TouchFrameEventHandler(Touch_FrameReported);事件

没触发一次事件 就会捕获到一个点集TouchPointCollection,

   TouchPointCollection pointCollection = e.GetTouchPoints(drawCanvas);

把这些点集的xy 保存到两个数组

使用TouchAction 枚举 来判读鼠标的动作up down  move

void Touch_FrameReported(object sender, TouchFrameEventArgs e)
      {
          int pointsNumber = e.GetTouchPoints(drawCanvas).Count;
          TouchPointCollection pointCollection = e.GetTouchPoints(drawCanvas);

          for (int i = 0; i < pointsNumber; i++)
          {
              if (pointCollection[i].Action == TouchAction.Down)
              {
                
                  preXArray[i] = pointCollection[i].Position.X;
                  preYArray[i] = pointCollection[i].Position.Y;
              }
              if (pointCollection[i].Action == TouchAction.Move)
              {
                  Line line = new Line();
                 
                  line.X1 = preXArray[i];
                  line.Y1 = preYArray[i];
                  line.X2 = pointCollection[i].Position.X;
                  line.Y2 = pointCollection[i].Position.Y;

                  line.Stroke = new SolidColorBrush(Colors.Black);
                  line.Fill = new SolidColorBrush(Colors.Black);
                  drawCanvas.Children.Add(line);

                  preXArray[i] = pointCollection[i].Position.X;
                  preYArray[i] = pointCollection[i].Position.Y;
              }
          }
      }

保存到媒体库:

private void Save_Click(object sender, RoutedEventArgs e)
       {
           MediaLibrary library = new MediaLibrary();//XNA的库 
           WriteableBitmap bitMap = new WriteableBitmap(drawCanvas, null);
           MemoryStream ms = new MemoryStream();
           Extensions.SaveJpeg(bitMap, ms, bitMap.PixelWidth,
                               bitMap.PixelHeight, 0, 100);//SYYTEM.MEDIA
           ms.Seek(0, SeekOrigin.Begin);
           library.SavePicture(string.Format("Images\\{0}.jpg",
                                              Guid.NewGuid()), ms);
       }

你可能感兴趣的:(windows phone)