WPF Image resizing and storing works on most images but fails to resize or save on others

前端 未结 1 1839
时光取名叫无心
时光取名叫无心 2021-01-15 21:09

I\'m trying to resize and save the 3 images as defined in the Page_load event.

Within method ResizeAndSave I have 2 methods I\'m trying: FastResiz

相关标签:
1条回答
  • 2021-01-15 22:10

    IMAGE 3 however, is saved in dimensions 625x441px

    That is because the image is slightly different from the other ones, its DPI (dots per inch) is 300 instead of 96. Its size in pixels is 3071 x 2172 but you are using the Width and Height properties, the size in inches with a unit of 1/96" which is 982.72 x 695.04 for this image. Fix this by using the PixelWidth and PixelHeight properties instead:

    Dim tbBitmap As New TransformedBitmap(bfPhoto, 
       New ScaleTransform(nWidth / bfPhoto.PixelWidth, nHeight / bfPhoto.PixelHeight, 0, 0))
    

    IMAGE 3 however, is not saved at all

    That doesn't add up completely, but you do have a critical bug in this statement:

    Dim strThumbnail As String = "success" + Date.Now.Second.ToString + ".png"
    

    This name is not sufficiently unique to ensure that you don't overwrite an existing file. And if the code is "fast" then Date.Now.Second will have the same value and your code overwrites a previous written image file. Note how this bug won't repro when you debug, that makes the code artificially slower and the second will be different.

    You'll need a better way to name the file, Guid.NewGuid.ToString() is a very good way for example, guaranteed to be unique. Or use a simple counter that you increment for each image. You do need to focus on cleanup.

    0 讨论(0)
提交回复
热议问题