Thursday, December 31, 2020

Image compression using C#

Sometimes, we need to compress the image files while maintaining the image quality. This can be achieved by the following C# implementation.

Usage: zoomImage(path, zoom).Save(destImagePath);

        private Image zoomImage(string fileName, float zoom = 0.25f)   // set quality to 1-100, eg 50

        {

            Image img = Image.FromFile(fileName);

            Image zoomedImage = new Bitmap(Image.FromFile(fileName), Convert.ToInt32(img.Width * zoom), Convert.ToInt32(img.Height * zoom));

            int quality = 100;

            var myImageCodecInfo = ImageCodecInfo.GetImageEncoders().Where(encoder => encoder.MimeType == "image/jpeg").FirstOrDefault();

            var myEncoderParameters = new EncoderParameters(1);

            myEncoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);


            var stream = new MemoryStream();

            zoomedImage.Save(stream, myImageCodecInfo, myEncoderParameters);

            var newImage = Image.FromStream(stream);

            var g = Graphics.FromImage(newImage);

            g.InterpolationMode = InterpolationMode.Default;

            g.DrawImage(newImage, new Rectangle(Point.Empty, newImage.Size), 0, 0,

              newImage.Width, newImage.Height, GraphicsUnit.Pixel, new ImageAttributes());

            return newImage;

        }

MySQL partitioning

Wednesday, July 31, 2019

How to solve the windows update hanging at "Gear" icon in Windows10?

Windows 10 needs to have windows update installed regularly.
Sometimes, updates are not getting downloaded without visible reasons.
One important service for Windows Update is "Windows Update Medic Service".


  • Open the Registry Editor by executing regedit command.
  • Go to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WaaSMedicSvc in registry editor.
  • Double-click on Start registry DWORD to modify its Value data in right pane.
  • Set the Value data to 4 to disable Windows Update Medic Service (1 to enable that service)
  • Click "OK". Close Registry Editor and restart the machine to make changes effective.

Monday, July 1, 2019

How to keep track the changes to JPA in Spring Boot?

We develop the LOB software of different domain and almost every app use backend database to persist the data. In Java, JPA is widely use for ORM layer in between of app and database. In many occasions, CRUD logging is part of the basic requirement.
One common scenario is application needs to keep track of price change on a particular stock item within a year. It start with $25 initially and increase to $30 in a month. After 3 months, it goes down a little bit to $28 due to excess supply. In following month, it goes back to $30 thanks to higher demand and etc.
Most probably, this kind of information is useful for the end-users and our application needs to support this.
Of course, we can implement this feature by hard-coding each updates to the database, can't we ???
But, we have better option with JPA Entity Listeners. We can easily bind the event listener and persist each changes to database. It is simple and easy to implement with the help of Spring Boot.
Create a new Spring Boot application. Assume we have the following in application.properties file.
spring.datasource.url = jdbc:mysql://localhost:3306/jpa-logger?useSSL=false
spring.datasource.username = test
spring.datasource.password = test
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto = create
Detail implementation is as followed.
This is REST controller which is for testing JPA logging feature

AutowireUtil class is used to auto-wire spring-beans inside JPA listener

This is the base class for all model which require the JPA logging for CRUD action.

This is Employee class which inherit from BaseModel.

This is the meta information of CRUD event to be logged into database.

This is the listener class which persist the CRUD meta information into database.

This class inject auto-wire the bean for listener.

EmployeeRepository.java

EventRepository.java

Saturday, April 27, 2019

How to enable image resizing inside contenteditable DIV (aka WYSIWYG editor) in Chrome?

When we put the image inside contenteditable DIV (aka WYSIWYG editor), IE allow resizing by default but Chrome does not. In fact, we can easily implement this features using pure Javascript. I compile DOM handling to mouse, mouseup and other event handler into a sample program.
Source code

GreenRed

Sunday, April 14, 2019

Paste the image into contenteditable div

When we accept the user input data, user input text data most of the time. But for some application, users need to input image and website needs to persist them. With the support of FileReader and clipboardData of paste event, we can easily enable that direct image input. User can directly copy the images from the software like MS-Paint or Web Browser and paste it into our input control (contenteditable div in our scenario).

Sample Text Editor with Image copy-paste support
Show HTML source
SampleImage

Image compression using C#

Sometimes, we need to compress the image files while maintaining the image quality. This can be achieved by the following C# implementation....