C# vb.net Implementation of gamma Gamma Adjustment Special Effect Filter

Posted by XPertMailer on Wed, 25 Sep 2019 04:40:20 +0200

In.net, how can you quickly and easily implement gamma gamma-adjusted special effects filters in the Photoshop filter group?The answer is to call SharpImage!Professional image special effect filters and synthetic libraries.Start by demonstrating the key code below, or you can download all the source code at the end:

Set Authorization

Step 1: After referencing SharpImage.dll, before calling the SharpImage method, be sure to set the authorization information with the code below. If you're in a trial version, type Test directly.

KeyMgr.SetKey("Test");

Initialize an ImageEngine

From a previous blog post, we introduced a picture to understand SharpImage. From that article, we can see that SharpImage uses ImageEngine as the cornerstone of image processing. All effects are processed as Effects of ImageEngine. Effects will be applied to various elements of ImageEngine, resulting in a myriad of changes.The effect of conversion.See the following code:

//Instantiate ImageEngine
engine = new ImageEngine();

Set basic parameters for ImageEngine

Some custom settings for ImageEngine can be found in the API manual.The code is as follows:

//Set some basic properties
engine.Canvas.AutoSize = true;
engine.Canvas.CenterElements = true;
engine.Canvas.Width = 320;//This is usually set to the width of the picture.
engine.Canvas.Height = 213;//This is usually set to the height of the picture.
engine.Canvas.Fill.Type = FillType.Solid;//Fill the canvas with solid colors
engine.Canvas.Fill.BackgroundColor = Color.White;//The canvas is white

Load pending pictures

SharpImage supports loading of pending pictures into memory in a variety of ways: from picture URL s, local paths, Bitmap objects, byte arrays, Base64 strings.Here are three ways of code:

#region Loads GDI+ Objects for Test Pictures
bmpDemoImage = Properties.Resources.demo;
#endregion

#region loads the byte stream of the test picture
using (MemoryStream ms = new MemoryStream())
{
    bmpDemoImage.Save(ms, bmpDemoImage.RawFormat);
    arrDemoImage = ms.ToArray();
}
#endregion

#region Load Base64 String of Test Picture
strBase64DemoImage = Convert.ToBase64String(arrDemoImage);
#endregion

Initialize ImageElement

In SharpImage, pictures are abstracted as ImageElements, and we bind the pending pictures to the ImageElements.The code is as follows:

//Create an ImageElement
imageEle = new ImageElement();

//Next, ImageElement is provided with data based on the specific type of picture data source, which is shown here as a manual selection using a ComboBox
switch (cmbBox11.SelectedIndex)
{
  case 0:
          //Picture URL
          imageEle.SourceType = ImageSource.File;
          imageEle.SourceFile = "http://www.zzsgzn.com/images/demo.jpg";
          break;
  case 1://Local Path
          imageEle.SourceType = ImageSource.File;
          imageEle.SourceFile = "c:\\demo.jpg";
          break;
  case 2://GDI+Object
          imageEle.SourceType = ImageSource.Image;
          imageEle.SourceImage = bmpDemoImage;
          break;
  case 3://Byte Array
          imageEle.SourceType = ImageSource.Binary;
          imageEle.SourceBinary = arrDemoImage;
          break;
  case 4://base64 string
          imageEle.SourceType = ImageSource.Base64String;
          imageEle.SourceBase64 = strBase64DemoImage;
          break;
}

Bind ImageElement and ImageEngine

To bind the ImageElment object to ImageEngine, simply execute the following code:

//Bind the ImageElement to the Image Engine object
engine.Elements.Add(imageEle);

At this point, it is important to note that engine.Elements can contain many elements, and the end result will be an overlay of these elements.This principle is the same as Photoshop's layer overlay.

Applying filters

Hooray!Ready to go!Apply the filter below, the gamma gamma adjustment filter is AdjustGammaEffect, instantiate it and apply it to ImageEngine, code as follows:

//Initialize the filter and add it to the ImageEngine's effect filter group
AdjustGammaEffect effect = new AdjustGammaEffect();
effect.Gamma = 10;

//Here you can also adjust the specific parameters of the filter by referring to the API manual
...


engine.Effects.Add(effect);

Get Processing Results

Once applied, you can get the processing results!

Image bmpResult = engine.GetOutputImage();

Running Effect Diagram

demo source download

Click to download source code

Related Recommendations

You may need to know how to get a camera frame picture or a desktop screen picture, please know SharpCapture:
SharpCapture, Desktop Screen, Camera, Audio and Video Collection Class Library

You may need to know how to adjust several dozen parameters, such as camera brightness, contrast, tone exposure, etc. Learn about SharpCamera:
SharpCamera, Professional Camera Advanced Parameters Deep Control Class Library