Windows phone 8.1 crop Image

I am new to Windows Phone programming. I am trying to build a face detection application. My problem is that I cannot properly crop the image to the rectangle that divides the face.

Here is the original and cropped image: https://onedrive.live.com/redir?resid=3F56C0D8DEC03C5B%21109

foreach (var r in faces)
{                                
    System.Windows.Shapes.Rectangle toAdd = new System.Windows.Shapes.Rectangle();                    
    TranslateTransform loc = new TranslateTransform();

    loc.X = r.X * _downsampleFactor / (double)w * cnvsFaceRegions.ActualWidth;
    loc.Y = r.Y * _downsampleFactor / (double)w * cnvsFaceRegions.ActualHeight;                     
    toAdd.RenderTransform = loc;
    toAdd.Width = r.Width * _downsampleFactor+50;
    toAdd.Height = r.Height * _downsampleFactor+50;
    toAdd.Stroke = new SolidColorBrush(Colors.Red);

    cnvsFaceRegions.Children.Add(toAdd);
    widthRectangle = toAdd.Width;
    heightRectangle = toAdd.Height;
    point1 = loc.X ;
    point2 = loc.Y ;

}

      

And here I am cropping the image:

 private  void  SaveScreenShots()            
 {             
   WriteableBitmap bmp = new WriteableBitmap((int)this.ActualWidth, (int)this.ActualHeight);           
   WriteableBitmap bmp2= CropImage(bmp,(int)point1, (int)point2, (int)widthRectangle, (int)heightRectangle);             
   bmp2.Render(this, null);
   byte[] bb = EncodeToJpeg(bmp2);
   bmp2.Invalidate();

   MemoryStream mem = new MemoryStream();
   bmp2.SaveJpeg(mem, bmp2.PixelWidth, bmp2.PixelHeight, 0, 100);
   mem.Seek(0, System.IO.SeekOrigin.Begin);

   if (mem != null)
   {

       MediaLibrary library = new MediaLibrary();
       try
       {

           pic = library.SavePicture("Mask_" + Guid.NewGuid().ToString(), mem);
           MaskPath = pic.GetPath();
           Deployment.Current.Dispatcher.BeginInvoke(() =>
           {
               MessageBoxResult result = MessageBox.Show("", "Saved successfully", MessageBoxButton.OK);

           });

       }
       catch (Exception ex)
       {
           MessageBox.Show("Unable to save the photo." + ex);

       }
   }     
        cameraViewer.NewCameraFrame += NewCameraFrame;
    }

      

Trim function:

 private static WriteableBitmap CropImage(WriteableBitmap source, int xOffset, int yOffset, int width, int height)
     {

         var sourceWidth = source.PixelWidth;
         var result = new WriteableBitmap(width, height);
         for (var x = 0; x <= height - 1; x++)
         {
             var sourceIndex = xOffset + (yOffset + x) * sourceWidth;
             var destinationIndex = x * width;
             Array.Copy(source.Pixels, sourceIndex, result.Pixels, destinationIndex, width);
         }
         return result;

     }

      

Thanks for the help.

+3


source to share


1 answer


Invalid implementation crop

.

var sourceIndex = xOffset + (yOffset + x) * sourceWidth;

      

You want an offset that reflects the equation for Line

y = mx + b;    // where y is your offset
               // m is your vertical position
               // x is your width
               // b is your starting horizontal position

      

So you basically copy the horizontal section of the image and copy those pixels to the buffer, repeat until you have enough sections.



Full implementation:

private WriteableBitmap CropImage(WriteableBitmap source, int x, int y, int width, int height)
{
    // range check
    if (x < 0 || y < 0 || width <= 0 || height <= 0)
        return null;

    // create the bitmap
    WriteableBitmap dest = new WriteableBitmap(width, height);

    // calculate the starting offset
    int offset = (y * source.PixelWidth) + x;

    // copy each row of pixels from the starting y location to (y + height) with the width of width
    for (int i = 0; i < height; i++)
    {
        Array.Copy(source.Pixels, offset, dest.Pixels, i * width, width);
        offset += source.PixelWidth;
    }

    // return the crop image
    return dest;
}

      


Additional links

How to crop an image using the WriteableBitmap class

0


source







All Articles