Module: Asposeimagingjava::SpecifyTransparencyForPngImage

Defined in:
lib/asposeimagingjava/images/specifytransparencyforpngimage.rb

Instance Method Summary collapse

Instance Method Details

#initializeObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/asposeimagingjava/images/specifytransparencyforpngimage.rb', line 3

def initialize()
    data_dir = File.dirname(File.dirname(File.dirname(File.dirname(__FILE__)))) + '/data/'

    # Load an existing image (of type bmp) in an instance of Image class

    image = Rjb::import('com.aspose.imaging.Image').load(data_dir + "sample.png")

    # Store the width & height in variables for later use

    width = image.getWidth()
    height = image.getHeight()

    # Load the pixels of RasterImage into the array of type Color

    pixels = image.loadPixels(Rjb::import('com.aspose.imaging.Rectangle').new(0, 0, width, height))

    # Create & initialize an instance of PngImage while specifying size and PngColorType

    png = Rjb::import('com.aspose.imaging.fileformats.png.PngImage').new(width, height, Rjb::import('com.aspose.imaging.fileformats.png.PngColorType').TruecolorWithAlpha)
    
    # Save the previously loaded pixels on to the new PngImage

    png.savePixels(Rjb::import('com.aspose.imaging.Rectangle').new(0, 0, width, height), pixels)

    # Set TransparentColor property to specify which color to be rendered as transparent

    png.setTransparentColor(Rjb::import('com.aspose.imaging.Color').getBlack())

    # Save the result on disc

    png.save(data_dir + "specify_transparency.png")

    # Display Status.

    puts "Specified transparency for PNG image successfully!"
end