Reduce JPG size

This forum is meant for questions and discussions about the X# language and tools
Post Reply
User avatar
Horst
Posts: 336
Joined: Tue Oct 13, 2015 3:27 pm

Reduce JPG size

Post 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
Attachments
ImageHelper.txt
(4.37 KiB) Downloaded 75 times
User avatar
Chris
Posts: 4910
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Reduce JPG size

Post 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
.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
Horst
Posts: 336
Joined: Tue Oct 13, 2015 3:27 pm

Reduce JPG size

Post 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
Attachments
ScaleImage.zip
(6.81 MiB) Downloaded 77 times
User avatar
Chris
Posts: 4910
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Reduce JPG size

Post by Chris »

Hi Horst,

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

XSharp Development Team
chris(at)xsharp.eu
Post Reply