Magick.NET icon indicating copy to clipboard operation
Magick.NET copied to clipboard

sample code for the Wiki

Open flemingm opened this issue 6 years ago • 1 comments

Prerequisites

  • [x] I have written a descriptive issue title
  • [x] I have verified that I am using the latest version of Magick.NET

System Configuration

  • Magick.NET version:
  • Environment (Operating system: Windows

Samples

Sample 1 -- List basic properties of input file (file = path of image).

  MagickReadSettings settings = new MagickReadSettings();
            settings.Verbose = true;
            settings.Debug = true;

 using (MagickImage image = new MagickImage(file, settings))
                        {
                            // Attach event handler to warning event
                            image.Warning += MagickImage_Warning;

                            Console.WriteLine("MagicKImage" + image.Format.ToString() + ", " + image.BaseWidth + ", " + image.BaseHeight +
                                ", Depth: " + image.Depth + ", Channels: " + image.ChannelCount +
                                ", ColorSpace:" + image.ColorSpace.ToString());
                            Console.WriteLine("MimeType: " + image.FormatInfo.MimeType +
                                ", Type: " + image.GetType().ToString() +
                                ", MultiFramed: " + image.FormatInfo.IsMultiFrame.ToString() +
                                 ", HasAlpha: " + image.HasAlpha.ToString());

                            Console.WriteLine("Comment:" + image.Comment);
}

Sample 2 - Color profile handling and conversion sample (sepia and grayscale).

/// ImageMagick.GetNextImageProperty(image); using (MagickImage image = new MagickImage(file, settings)) { /// ============================ COLOR PRofile testing ///
ColorProfile icc = image.GetColorProfile(); ImageMagick.ColorProfile profile = null; String dest = null; image.Comment = "mark test"; String CMYKProfilePath = "e:\Images\jpg\Test CMYK Profile.icc"; String GRAYProfilePath = "e:\Images\jpg\Test GRAY Profile.icc"; String RGBProfilePath = "e:\Images\jpg\Test RGB Profile.icc"; String baseName = Path.GetFileNameWithoutExtension(file); String basePath = Path.GetFullPath(file);

                        Console.WriteLine("\nColor profile: {0}, colorSpace {1}", icc.Name, icc.ColorSpace);
                        ImageMagick.ColorSpace sourceImageColorSpace = image.ColorSpace;

                        //Check if image color space is of type CMYK, add color profile accordingly
                        if (sourceImageColorSpace == ImageMagick.ColorSpace.CMYK)
                        {
                            
                            //Adding of other color profiles code commented till the time the issue of multiple profile addition resolved
                            //_profilesCMYK is the list containing the ICSProfiles
                            if (CMYKProfilePath != "")
                            {   //Create a new color profile object according to selection and add it into image
                                IMagickImage clone = image.Clone();
                                clone.TransformColorSpace(ColorProfile.AdobeRGB1998);   // input must have a color profile
                                
                                // used file saved on disk...
                                profile = new ImageMagick.ColorProfile(CMYKProfilePath);
                                image.AddProfile(profile, true);    // true = replace profile with selected one (ie. path)
                                image.TransformColorSpace(ColorProfile.AdobeRGB1998);

                                image.Format = MagickFormat.Jpeg;
                                // Save the result
                                dest = "e:\\images\\jpg\\"+baseName +"-cmykToRGB.jpg";
                                image.Write(dest);
                         
                                dest = "e:\\images\\jpg\\" + baseName + "-cmykToRGBusingInput.jpg";
                                clone.TransformColorSpace(ColorProfile.AdobeRGB1998);   // convert to RGB applying input profile
                                clone.Write(dest);
                                
                                // test converting to Gray scale..
                                IMagickImage Sepia = image.Clone();
                                Sepia.SepiaTone();
                                dest = "e:\\images\\jpg\\" + baseName + "-cmykToSepia.jpg";
                                Sepia.Write(dest);
                                
                                // test converting to Gray scale..
                                IMagickImage grey = image.Clone();
                                grey.Grayscale();
                                dest = "e:\\images\\jpg\\" + baseName + "-cmykToGray.jpg";
                                grey.Write(dest);

                                grey.Format = MagickFormat.Png;
                                dest = "e:\\images\\jpg\\" + baseName + "-cmykToGray.png";
                                grey.Write(dest);
                                grey.Format = MagickFormat.Tiff;
                                dest = "e:\\images\\jpg\\" + baseName + "-cmykToGray.tiff";
                                grey.Write(dest);

                                // Test stripping all profiles

                                dest = "e:\\images\\jpg\\" + baseName + "-cmykToRGBNoProfiles.jpg";
                                image.Strip();
                                image.Write(dest);
                                continue;
                            }//End if
                        }

                        //Check if image color space is of type RGB, add color profile accordingly
                        else if (sourceImageColorSpace == ImageMagick.ColorSpace.RGB ||
                                 sourceImageColorSpace == ImageMagick.ColorSpace.sRGB)
                        {
                            image.Format = MagickFormat.Jpeg;
                            image.BorderColor = MagickColor.FromRgb(255, 0, 0);
                            image.Border(5);

                            // Save the result
                            dest = "e:\\images\\jpg\\" + baseName + "-isRGB.jpg";
                            image.TransformColorSpace(ColorProfile.AdobeRGB1998);       // Convert to RGB using input Profile.
                            image.Write(dest);

                            dest = "e:\\images\\jpg\\" + baseName + "-isRGBSepia.jpg";
                            image.SepiaTone();
                            image.Write(dest);

                            //ICSPrfile is the object containing the name and path of availabe prfiles
                            //_profilesRGB is the list containing the ICSProfiles
                            if (RGBProfilePath != "")
                            {   //Create a new color profile object according to selection and add it into image
                              //  profile = new ImageMagick.ColorProfile(RGBProfilePath);
                               // image.AddProfile(profile, true);    // Overwrite Existing.
                            }//End if
                            continue;
                        }

                        //Check if image color space is of type GRAY, add color profile accordingly
                        else if (sourceImageColorSpace == ImageMagick.ColorSpace.Gray)
                        {
                            if (GRAYProfilePath != "")
                            {   //Create a new color profile object according to selection and add it into image
                               profile = new ImageMagick.ColorProfile(GRAYProfilePath);
                                image.AddProfile(profile, true);        // Replace input profile (not applying input)
                                image.TransformColorSpace(ColorProfile.AdobeRGB1998);   // convert to RGB.

                                image.Format = MagickFormat.Jpeg;
                                // Save the result
                                dest = "e:\\images\\jpg\\GrayToRGB.jpg";
                                image.Write(dest);
                                continue;
                            }//End if
                        }

}

flemingm avatar May 29 '19 04:05 flemingm

Sample file used in sample code:

TestCMYK TestGray TestRGB

flemingm avatar May 29 '19 04:05 flemingm