Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have my image cropping app based on:
http://www.c-sharpcorner.com/UploadFile/55275a/windowsphone-image-crop-with-rectangle/

I modified it a little bit so I can resize rectangle instead of creating new

so my whole method look like:

C#
private async void Accept_Click(object sender, EventArgs e)
  {
      WriteableBitmap wb = new WriteableBitmap(image);

      double originalImageWidth = wb.PixelWidth;
      double originalImageHeight = wb.PixelHeight;

      double displayedWidth = image1.ActualWidth;
      double displayedHeight = image1.ActualHeight;

      double widthRatio = originalImageWidth / displayedWidth;
      double heightRatio = originalImageHeight / displayedHeight;

      r = (Rectangle) (from c in LayoutRoot.Children
                       where c.Opacity == .5 select c).First();

      GeneralTransform gt = r.TransformToVisual(LayoutRoot);
      Point p = gt.Transform(new Point(0, 0));

      Point1 = (r.TransformToVisual(this)).Transform(new Point(0, 0));
      Point2 = new Point(Point1.X + r.Width, Point1.Y + r.Height);

      WriteableBitmap newImage = new WriteableBitmap(
          (int) (widthRatio * Math.Abs(Point2.X - Point1.X)),
          (int) (heightRatio * Math.Abs(Point2.Y - Point1.Y)));

      int xoffset = (int) (((Point1.X < Point2.X) ? Point1.X : Point2.X) * widthRatio);
      int yoffset = (int) (((Point1.Y < Point2.Y) ? Point1.Y : Point2.X) * heightRatio);

      if (newImage.Pixels.Length > 0)
      {
          for (int i = 0; i < newImage.Pixels.Length; i++)
          {
              int x = (int) ((i % newImage.PixelWidth) + xoffset);
              int y = (int) ((i / newImage.PixelWidth) + yoffset);
              newImage.Pixels[i] = wb.Pixels[y * wb.PixelWidth + x];
          }

          using (MemoryStream ms = new MemoryStream())
          {
              newImage.SaveJpeg(ms, (int) newImage.PixelWidth,
                  (int) newImage.PixelHeight, 0, 100);

              image.SetSource(ms);
          }
      }
      else
      {
      }

      ProgressBar pb = new ProgressBar();
      pb.IsEnabled = true;
      LayoutRoot.Children.Add(pb);
      int idReceipt = (int) PhoneApplicationService.Current.State["paragon"];
      await ReceiptsHelper.addPhotosToReceipt(image, idReceipt);
      NavigationService.Navigate(new Uri("/7.0/StronaParagonu.xaml", UriKind.Relative));
  }


When my image is vertical everything works fine, but when my image is horizontal I get Array Index Out Of Bounds Exception at:

newImage.Pixels[i] = wb.Pixels[y * wb.PixelWidth + x];

I don't know exactly what am I doing wrong.

Anybody can help?



EDIT:
I changed my code to:

C#
if (wb.PixelWidth > wb.PixelHeight)     {
    for (int i = 0; i < newImage.Pixels.Length; i++)
    {
        int x = (int)((i % newImage.PixelWidth) + xoffset);
        int y = (int)((i / newImage.PixelWidth) + yoffset);
         newImage.Pixels[i] = wb.Pixels[x * wb.PixelHeight + y];
    }
}
else
{
    for (int i = 0; i < newImage.Pixels.Length; i++)
    {
        int x = (int)((i % newImage.PixelWidth) + xoffset);
        int y = (int)((i / newImage.PixelWidth) + yoffset);
        newImage.Pixels[i] = wb.Pixels[y * wb.PixelWidth + x];
    }
}




In result I've got something like [this](https://assets.okazjum.pl/uploads/uploads/receipt_photo/file/88/s3_2015_9_46_08_PM.jpg)


Maybe there is some workaround in which I can flip image 90 degree?
I tried WriteableBitmapEx but it doesnt work.
Posted
Updated 20-Mar-15 17:56pm
v2
Comments
Sergey Alexandrovich Kryukov 20-Mar-15 19:07pm    
Apparently, the index can go out of range in wb.Pixels[*], you incorrectly calculate the index. The problem looks very simple. You can easily find it out using the debugger.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900