locked
filesize increase after image rotated. RRS feed

  • Question

  • User944339287 posted

    hi guys.. the following url is the article i refer to rotate image and save to directgory.

    how can i avoid the file size increase? i test with rotate an image and its changed from 900KB to 12.1MB. any idea?

    https://asp-net-example.blogspot.com/2012/03/how-to-rotate-image-in-aspnet.html

    Dim path As String = Server.MapPath(Context.Request.ApplicationPath & "/upload/test.jpg")
    Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(path)
    img.RotateFlip(RotateFlipType.Rotate90FlipNone)
    img.Save(path)



    Monday, August 19, 2019 11:53 AM

Answers

  • User665608656 posted

    Hi kengkit,

    According to your description, you want to make the rotated image smaller rather than larger, right?

    If so , you can try this code:

        Dim path As String = Server.MapPath("~/Images1/pic4.jpg")
        Dim pic As Bitmap = New Bitmap(path)
        Dim pic2 As Bitmap = New Bitmap(pic.Width, pic.Height, PixelFormat.Format32bppRgb)
        Dim rawFormat = pic.RawFormat
    
        For x As Integer = 0 To pic.Width - 1
    
            For y As Integer = 0 To pic.Height - 1
                pic2.SetPixel(x, y, pic.GetPixel(x, y))
            Next
        Next
    
        pic2.RotateFlip(RotateFlipType.Rotate90FlipNone)
        pic2.Save(path , rawFormat)

    You can refer to this link: https://stackoverflow.com/a/33188884

    If you want change the size ,you can change  PixelFormat.Format32bppRgb to others, you can refer to this link: PixelFormat Enum

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 20, 2019 8:55 AM