Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Dewayne Thomas
PLUS
Dewayne Thomas
Courses Plus Student 10,283 Points

A subject line of image not saving with new dimensions. Xamarin c#

I am taking a photo with my app then rotating if needed then resizing. After that I am saving the new rotated and scaled down picture to the sd card. The photo does rotate and the byte size changes but when i view the new photo it still has the same dimensions as the orginal photo. Please help. My code is below.

my resizing and saving calls are //resize for preview using (var bitmap = _file.Path.LoadAndResizeBitmap(300, 225)) { //resize for saving. Bitmap resized = _file.Path.LoadAndResizeBitmap(800, 600);

                    BitmapHelpers.saveBitmap(resized,_user_id,_dateStr);

                    //preview image
                    photo.SetImageBitmap(ConvertToBitmap(bitmap));

                }


    public static Bitmap LoadAndResizeBitmap(this string fileName, int width, int height)
    {
        //First we get the diamensions of the file on disk
        BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true };
        BitmapFactory.DecodeFile(fileName, options);

        //Next we calculate the ratio that we need to resize the image by
        //in order to fit the requested dimensions.
        int outHeight = options.OutHeight;
        int outWidth = options.OutWidth;
        int inSampleSize = 1;

        if (outHeight > height || outWidth > width)
        {
            inSampleSize = outWidth > outHeight
                ? outHeight / height : outWidth / width;
        }

        //Now we will load the image
        options.InSampleSize = inSampleSize;
        options.InJustDecodeBounds = false;
        Bitmap resizedBitmap = BitmapFactory.DecodeFile(fileName, options);

        // Images are being saved in landscape, so rotate them back to portrait if they were taken in portrait
        Matrix mtx = new Matrix();
        Android.Media.ExifInterface exif = new Android.Media.ExifInterface(fileName);
        string orientation = exif.GetAttribute(Android.Media.ExifInterface.TagOrientation);

        switch (orientation)
        {
            case "6": // portrait
                mtx.PreRotate(90);
                resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false);
                mtx.Dispose();
                mtx = null;
                break;
            case "1": // landscape
                //mtx.PreRotate(-90);
                //resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false);
                //mtx.Dispose();
                //mtx = null;
                break;
            default:
                mtx.PreRotate(90);
                resizedBitmap = Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, mtx, false);
                mtx.Dispose();
                mtx = null;
                break;
        }
                return resizedBitmap;
    }


    public static void saveBitmap(Bitmap bitmap, int userId, String dateStr)
    {

        var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
        String fileName = "bb_" + userId + "_" + dateStr + "_new.jpg";
        var filePath = System.IO.Path.Combine(sdCardPath, fileName);
        var stream = new FileStream(filePath, FileMode.Create);
        bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
        stream.Close();

    }

when it does the first _file.Path.LoadAndResizeBitmap(300, 225) the inSampleSize is 3 and returns new height,width of 640,360 when it does _file.Path.LoadAndResizeBitmap(800, 600) the inSampleSize is 1 and returns original values of 1280,720

1 Answer

Jeremy McLain
STAFF
Jeremy McLain
Treehouse Guest Teacher

We don't yet teacher Xamarin mobile app development at Treehouse. You've have much better luck if you post this question to StackOverflow