Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

Reduce JPG size 28 Jan 2023 11:54 #25081

  • Horst
  • Horst's Avatar
  • Topic Author


  • Posts: 293
  • Hello
    I found a c# example to reduce the size of jpg files and translated it to x#.
    Now i was using it also for my photocollection to reduce discspace and loading time over the web.
    Now i have the problem, that photos in portrait format are turned 90 degree . With photos in landscape format it doesnt happend.

    With google i found the solution in c# but now i stock to translate the example.
    CLASS ImageHelper
        METHOD RotateFlipType RotateImageByExifOrientationData (oImage AS Image , updateExifData AS LOGIC)
        	LOCAL orientationId := 0x0112 AS INT
    
            VAR fType := RotateFlipType.RotateNoneFlipNone
            IF (oImage:PropertyIdList.Contains(orientationId))
            
                VAR pItem := oImage.GetPropertyItem(orientationId)
                fType := GetRotateFlipTypeByExifOrientationData(pItem.Value[0])
                IF (fType != RotateFlipType.RotateNoneFlipNone)
                    oImage.RotateFlip(fType)
                    // Remove Exif orientation tag (if requested)
                    IF (updateExifData) 
                    	oImage.RemovePropertyItem(orientationId)
                    ENDIF
               ENDIF
            ENDIF
            RETURN fType
    
        METHOD RotateFlipType GetRotateFlipTypeByExifOrientationData(orientation AS INT)
            SWITCH (orientation)
                CASE 1 ; //            default:?? = otherwise ;               RETURN RotateFlipType.RotateNoneFlipNone
                CASE 2;                RETURN RotateFlipType.RotateNoneFlipX
                CASE 3;                RETURN RotateFlipType.Rotate180FlipNone
                CASE 4;                RETURN RotateFlipType.Rotate180FlipX
                CASE 5;                RETURN RotateFlipType.Rotate90FlipX
                CASE 6;                RETURN RotateFlipType.Rotate90FlipNone
                CASE 7;                RETURN RotateFlipType.Rotate270FlipX
                CASE 8;                RETURN RotateFlipType.Rotate270FlipNone
            END SWITCH
    
      END CLASS

    1: How to handle this -> RotateFlipType.
    2. Default in the swith ?
    3. RotateImageByExifOrientationData = Constructor ?
    I know, not much code but hard for me.
    Horst

    c# source in the zip
    Attachments:

    Please Log in or Create an account to join the conversation.

    Reduce JPG size 28 Jan 2023 19:21 #25087

    • Chris
    • Chris's Avatar


  • Posts: 3856
  • Hi Horst,

    "RotateFlipType" is an enum defined in System.Drawing.dll and its full name is "System.Drawing.RotateFlipType". It's the return type of the methods.
    In X#, you would use OTHERWISE instead of c#'s default in a switch statement.

    The X# version of the code is this:

    USING System.Drawing
    USING System.Drawing.Drawing2D
    USING System.Drawing.Imaging
    USING System.Linq
    
    CLASS ImageHelper
    	STATIC METHOD RotateImageByExifOrientationData (oImage AS Image , updateExifData AS LOGIC) AS RotateFlipType 
    		LOCAL orientationId := 0x0112 AS INT
    		
    		LOCAL fType AS RotateFlipType
    		fType := RotateFlipType.RotateNoneFlipNone
    		IF oImage:PropertyIdList:Contains(orientationId)
    			
    			LOCAL pItem AS System.Drawing.Imaging.PropertyItem
    			pItem := oImage:GetPropertyItem(orientationId)
    			fType := GetRotateFlipTypeByExifOrientationData(pItem:Value[0])
    			IF fType != RotateFlipType.RotateNoneFlipNone
    				oImage:RotateFlip(fType)
    				// Remove Exif orientation tag (if requested)
    				IF (updateExifData) 
    					oImage:RemovePropertyItem(orientationId)
    				ENDIF
    			ENDIF
    		ENDIF
    	RETURN fType
    	
    	STATIC METHOD GetRotateFlipTypeByExifOrientationData(orientation AS INT) AS RotateFlipType 
    		SWITCH orientation
    		CASE 1
    			RETURN RotateFlipType.RotateNoneFlipNone
    		CASE 2
    			RETURN RotateFlipType.RotateNoneFlipX
    		CASE 3
    			RETURN RotateFlipType.Rotate180FlipNone
    		CASE 4
    			RETURN RotateFlipType.Rotate180FlipX
    		CASE 5
    			RETURN RotateFlipType.Rotate90FlipX
    		CASE 6
    			RETURN RotateFlipType.Rotate90FlipNone
    		CASE 7
    			RETURN RotateFlipType.Rotate270FlipX
    		CASE 8
    			RETURN RotateFlipType.Rotate270FlipNone
    		OTHERWISE
    			RETURN RotateFlipType.RotateNoneFlipNone
    		END SWITCH
    END CLASS

    and you need to have references in your app to System.Drawing and to System.Core for it to compile.

    Please note that this code rotates the image only when it finds appropriate data in the image header. If you want to ALWAYS rotate the image, then you can do it with just one line of code :)

    oImage:RotateFlip(RotateFlipType.Rotate90FlipNone) // rotate by 90 degrees

    .
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Last edit: by Chris.

    Reduce JPG size 31 Jan 2023 15:54 #25113

    • Horst
    • Horst's Avatar
    • Topic Author


  • Posts: 293
  • Hello Chris

    Thank you, with your help i was be able to make it working.

    In the attachment the whole little program. Maybe somebody can use it also.

    Horst
    Attachments:

    Please Log in or Create an account to join the conversation.

    Reduce JPG size 01 Feb 2023 23:06 #25133

    • Chris
    • Chris's Avatar


  • Posts: 3856
  • Hi Horst,

    Great to hear and thanks for posting the full working code!
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1