xsharp.eu • Reduce JPG size
Page 1 of 1

Reduce JPG size

Posted: Sat Jan 28, 2023 10:54 am
by Horst
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.

Code: Select all


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

Reduce JPG size

Posted: Sat Jan 28, 2023 6:21 pm
by Chris
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:

Code: Select all

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 :)

Code: Select all

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

Reduce JPG size

Posted: Tue Jan 31, 2023 2:54 pm
by Horst
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

Reduce JPG size

Posted: Wed Feb 01, 2023 10:06 pm
by Chris
Hi Horst,

Great to hear and thanks for posting the full working code!