Question Resizing an image using bitmapdecoder

marcd2k13

New member
Joined
Apr 4, 2013
Messages
2
Programming Experience
5-10
Hi,


I am trying to resize any image to 150x150 using the code below, but when I use it, the resulting image is the right size but is completely corrupted, it is just a series of horizontal lines through which you can just about make out the image. Please could someone have a look at this code and see where i've gone wrong?


Many thanks


Marc


VB.NET:
 Dim Squarefile As Windows.Storage.StorageFile = Await StorageFile.GetFileFromPathAsync(txtSquareImage.Text)


            Dim newfile = Await Squarefile.CopyAsync(ApplicationData.Current.LocalFolder, sb.ToString & Squarefile.FileType, NameCollisionOption.ReplaceExisting)




            Dim sourceStream = Await newfile.OpenAsync(FileAccessMode.Read)
            Dim decoder = Await BitmapDecoder.CreateAsync(sourceStream)


                       Dim pixelData = Await decoder.GetPixelDataAsync


            Dim destinationStream = Await newfile.OpenAsync(FileAccessMode.ReadWrite)


            Dim encoder = Await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, destinationStream)
            encoder.SetPixelData(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, 150, 150, decoder.DpiX, decoder.DpiY, pixelData.DetachPixelData)
            Await encoder.FlushAsync()
 
have solved this myself using a different approach:

VB.NET:
Dim sourceFileStream = Await sourceImage.OpenAsync(Windows.Storage.FileAccessMode.Read)
        Dim destFileStream = Await destImage.OpenAsync(FileAccessMode.ReadWrite)
        '
        Dim decoder = Await BitmapDecoder.CreateAsync(sourceFileStream)
        Dim enc = Await BitmapEncoder.CreateForTranscodingAsync(destFileStream, decoder)


        Debug.WriteLine(TileType)
        '  scale the bitmap to fit
        If TileType = "square" Then


            enc.BitmapTransform.ScaledHeight = 150
            enc.BitmapTransform.ScaledWidth = 150
        End If


        If TileType = "wide" Then
            enc.BitmapTransform.ScaledHeight = 150
            enc.BitmapTransform.ScaledWidth = 310
        End If




        ' write out to the stream
        Await enc.FlushAsync


        Await destFileStream.FlushAsync


        sourceFileStream.Dispose()
        destFileStream.Dispose()
 
Back
Top