1

I am trying to port my app in windows phone . i have to upload an image on server So it is in small Size For uploading i have done this thing in Widows Successfully but problem is when i failed in it .. here is my code for windows App

public void CompressImage(int i, int j)
        {
            bmp1.SetPixel(j, i, Color.FromArgb(bmp.GetPixel(j, i).R, bmp.GetPixel(j, i).G, bmp.GetPixel(j, i).B));   
        }



        private void bLoadImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            if (file.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = new Bitmap(file.FileName);
            }
        }


        private void bCompression_Click(object sender, EventArgs e)
        {
            bmp = new Bitmap(pictureBox1.Image);
            bmp1 = new Bitmap(bmp.Width, bmp.Height);
            for (int i = 1; i < bmp.Height; i++)
                for (int j = 1; j < bmp.Width; j++)
                {
                    CompressImage(i, j);
                }
            pictureBox2.Image = bmp1;
            bmp1.Save("Picture.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
        }

After Searching on google i found out that windows Phone does not support Bitmap .. any idea how i can do the same thing in windows phone or any other alternative for doing this

Mashhood Adeeb
  • 37
  • 2
  • 11

4 Answers4

1

You should use WriteablBitmap to reduce size of image. WriteablBitmap has number of methods for images in windows phone Here is more about writeablebitmapex.

Jaihind
  • 2,770
  • 1
  • 12
  • 19
  • yes i have seen this link before when i am create an object of WriteableBitmap i see very few method in it .. – Mashhood Adeeb Mar 04 '14 at 06:25
  • @MashhoodAdeeb WriteableBitmapExtensions has Resize method that is helpful for you. – Jaihind Mar 04 '14 at 06:54
  • this is the error i am getting while adding its reference to my project **A reference to higher version or in compatibility assembly cannot be added** @Jaihind WriteableBitmapExtensions – Mashhood Adeeb Mar 04 '14 at 07:13
  • @MashhoodAdeeb use 'Install-Package WriteableBitmapEx' nuget command to add refrence. – Jaihind Mar 04 '14 at 07:32
0

When you are taking picture you can choose the resolution with which the picture will be taken. This can be done by

PhotoCamera cam; 

After camera initizalition.

Following code when image is capturing (in the method that captures the image)

IEnumerable<Size> resList = cam.AvailableResolutions;

Size res;
if (resList.Count() > 0)
{
    res = resList.ElementAt<Size>(0);
    cam.Resolution = res;

 }

This sample chooses the first resolution

Jagath Murali
  • 515
  • 3
  • 13
  • Sorry i did not understand your answer should i have to set the properties of camera before it capture an image .... Or should it reduce the size of image ?? – Mashhood Adeeb Mar 04 '14 at 06:31
  • Yes, you can set the camera properties after camera initialization(Resolution in this case) to capture the image in small resolution which will automatically give you image of smaller size – Jagath Murali Mar 04 '14 at 06:36
0

Try to load your original image to WriteableBitmap object, then you can use SaveJpeg() extension method from System.Windows.Media.Imaging namespace, to save new image with reduced size. For example :

.......
WriteableBitmap wb = new WriteableBitmap(bitmapImageObject);
wb.SaveJpeg(stream, 120, 160, 0, 100);
.......
har07
  • 88,338
  • 12
  • 84
  • 137
0

You can try this. It worked for me. It reduced my 9.70MB file into 270KB.

WriteableBitmap cameraCapturedImage = PictureDecoder.DecodeJpeg(e.ChosenPhoto, 1024, 1024);

using (IsolatedStorageFileStream myFileStream = myStore.CreateFile(fileName))
{
  System.Windows.Media.Imaging.Extensions.SaveJpeg(cameraCapturedImage, myFileStream, cameraCapturedImage.PixelWidth, cameraCapturedImage.PixelHeight, 0, 85);
  myFileStream.Close();
}

N.B: fileName is the name of file to save size reduced image.

reza.cse08
  • 5,938
  • 48
  • 39