You are on page 1of 23

Računarska grafika

GDI+
(Graphics Device Interface Plus)
Bitmaps
• A bitmap is an array of bits that specify the color of each pixel in a
rectangular array of pixels.
Bits per Number of colors that can be
pixel assigned to a pixel
1 2^1 = 2
2 2^2 = 4
4 2^4 = 16
8 2^8 = 256
16 2^16 = 65,536
24 2^24 = 16,777,216
Bitmaps
• a file might also contain a color table (sometimes called a color
palette). A color table maps numbers in the bitmap to specific
colors.
Graphics File Formats
• There are many standard formats for saving
bitmaps in disk files. GDI+ supports following
formats:
▫ BMP
▫ Graphics Interchange Format (GIF)
▫ Joint Photographic Experts Group (JPEG)
▫ Exchangeable Image File (EXIF)
▫ Portable Network Graphics (PNG)
▫ Tag Image File Format (TIFF)
BMP
• BMP is a standard format used by Windows to
store device-independent and application-
independent images. The number of bits per
pixel (1, 4, 8, 15, 24, 32, or 64) for a given BMP
file is specified in a file header. BMP files with
24 bits per pixel are common. BMP files are
usually not compressed and, therefore, are not
well suited for transfer across the Internet.
GIF
• GIF is a common format for images that appear on
Web pages. GIFs work well for line drawings,
pictures with blocks of solid color, and pictures with
sharp boundaries between colors. GIFs are
compressed, but no information is lost in the
compression process; a decompressed image is
exactly the same as the original. One color in a GIF
can be designated as transparent, so that the image
will have the background color of any Web page that
displays it. A sequence of GIF images can be stored
in a single file to form an animated GIF. GIFs store
at most 8 bits per pixel, so they are limited to 256
colors.
JPEG
• JPEG is a compression scheme that works well
for natural scenes such as scanned photographs.
Some information is lost in the compression
process, but often the loss is imperceptible to the
human eye. JPEGs store 24 bits per pixel, so
they are capable of displaying more than 16
million colors. JPEGs do not support
transparency or animation.
JPEG
• The level of compression in JPEG images is configurable, but higher
compression levels (smaller files) result in more loss of information.
A 20:1 compression ratio often produces an image that the human
eye finds difficult to distinguish from the original.
BMP & JPEG & GIF
• JPEG compression does not work well for line drawings, blocks of
solid color, and sharp boundaries. The JPEGs and the GIF were
compressed from the BMP. Note that the GIF maintains the sharp
boundaries along the lines, but the JPEGs tend to blur the
boundaries.
JFIF
• JPEG is a compression scheme, not a file format.
JPEG File Interchange Format (JFIF) is a file
format commonly used for storing and
transferring images that have been compressed
according to the JPEG scheme. JFIF files
displayed by Web browsers use the .jpg
extension.
EXIF
• EXIF is a file format used for photographs
captured by digital cameras. An EXIF file
contains an image that is compressed according
to the JPEG specification. An EXIF file also
contains information about the photograph
(date taken, shutter speed, exposure time, and so
on) and information about the camera
(manufacturer, model, and so on).
PNG
• The PNG format retains many of the advantages of the GIF
format but also provides capabilities beyond those of GIF.
Like GIF files, PNG files are compressed with no loss of
information. PNG files can store colors with 8, 24, or 48 bits
per pixel and grayscales with 1, 2, 4, 8, or 16 bits per pixel. In
contrast, GIF files can use only 1, 2, 4, or 8 bits per pixel. A
PNG file can also store an alpha value for each pixel, which
specifies the degree to which the color of that pixel is blended
with the background color.
• PNG improves on GIF in its ability to progressively display an
image (that is, to display better and better approximations of
the image as it arrives over a network connection). PNG files
can contain gamma correction and color correction
information so that the images can be accurately rendered on
a variety of display devices.
TIFF
• TIFF is a flexible and extendable format that is
supported by a wide variety of platforms and image-
processing applications. TIFF files can store images
with an arbitrary number of bits per pixel and can
employ a variety of compression algorithms. Several
images can be stored in a single, multiple-page TIFF
file. Information related to the image (scanner
make, host computer, type of compression,
orientation, samples per pixel, and so on) can be
stored in the file and arranged through the use of
tags. The TIFF format can be extended as needed by
the approval and addition of new tags.
DrawImage
• DrawImage(Image image, Point)
• DrawImage(Image image, Point[] dest)
• DrawImage(Image image, Rectangle)
• DrawImage(Image image, Point[] dest, Rectangle srcRect, GraphicsUnit srcUnit,
ImageAttributes imageAttr)

• image
▫ Image to draw.
• dest
▫ Array of three Point structures that define a parallelogram.
• srcRect
▫ structure that specifies the portion of theimage object to draw.
• srcUnit
▫ Member of the GraphicsUnit enumeration that specifies the units of measure used by
the srcRectparameter.
• imageAttr
▫ ImageAttributes that specifies recoloring and gamma information for the image object.
DrawImage
g.DrawImage(Image.FromFile(@"c:\cym.jpg"), 50, 50);

g.DrawImage(Image.FromFile(@"c:\cym.jpg"), new
Rectangle(50,50,200,200));

g.DrawImage(Image.FromFile(@"c:\cym.jpg"), new
Rectangle(50,50,200,200), new
Rectangle(100,100,150,150), GraphicsUnit.Pixel);

g.DrawImage(Bitmap.FromFile(@"c:\cym.jpg"), new
Rectangle(50,50,200,200), new
Rectangle(100,100,150,150), GraphicsUnit.Pixel);
Image
Name Description
Flags Gets attribute flags for the pixel data of this Image.
FrameDimensionsList Gets an array of GUIDs that represent the dimensions of frames within
thisImage.
Height Gets the height, in pixels, of this Image.
HorizontalResolution Gets the horizontal resolution, in pixels per inch, of thisImage.
Palette Gets or sets the color palette used for thisImage.
PhysicalDimension Gets the width and height of this image.
PixelFormat Gets the pixel format for this Image.
PropertyIdList Gets IDs of the property items stored in this Image.
PropertyItems Gets all the property items (pieces of metadata) stored in this Image.
RawFormat Gets the file format of this Image.
Size Gets the width and height, in pixels, of this image.
Tag Gets or sets an object that provides additional data about the image.
VerticalResolution Gets the vertical resolution, in pixels per inch, of thisImage.
Width Gets the width, in pixels, of this Image.
Image
Name Description
Clone Creates an exact copy of this Image.
FromFile(String) Creates an Imagefrom the specified file.
FromFile(String, Boolean) Creates an Imagefrom the specified file using embedded color management information in that file.
GetBounds Gets the bounds of the image in the specified unit.
GetEncoderParameterList Returns information about the parameters supported by the specified image encoder.
GetFrameCount Returns the number of frames of the specified dimension.
GetPixelFormatSize Returns the color depth, in number of bits per pixel, of the specified pixel format.
GetPropertyItem Gets the specified property item from this Image.
GetThumbnailImage Returns a thumbnail for this Image.
IsAlphaPixelFormat Returns a value that indicates whether the pixel format for this Image contains alpha information.
IsCanonicalPixelFormat Returns a value that indicates whether the pixel format is 32 bits per pixel.
IsExtendedPixelFormat Returns a value that indicates whether the pixel format is 64 bits per pixel.
RemovePropertyItem Removes the specified property item from thisImage.
RotateFlip Rotates, flips, or rotates and flips theImage.
Save(String) Saves this Image to the specified file or stream.
Save(String, ImageFormat) Saves this Image to the specified file in the specified format.
Save(String, Saves this Image to the specified file, with the specified encoder and image-encoder parameters.
ImageCodecInfo,
EncoderParameters)
SelectActiveFrame Selects the frame specified by the dimension and index.
SetPropertyItem Stores a property item (piece of metadata) in thisImage.
Image
using (Bitmap background = (Bitmap)Bitmap.FromFile("c:\\pozadina.png"))
{
using (Bitmap foreground = (Bitmap)Bitmap.FromFile(@"c:\tekst.png"))
{
if (background.Height == foreground.Height & background.Width == foreground.Width)
{
using (Bitmap mergedImage = new Bitmap(background.Width, background.Height))
{
for (int x = 0; x < mergedImage.Width; x++)
{
for (int y = 0; y < mergedImage.Height; y++)
{
Color backgroundPixel = background.GetPixel(x, y);
Color foregroundPixel = foreground.GetPixel(x, y);
Color mergedPixel = Color.FromArgb(backgroundPixel.ToArgb() & foregroundPixel.ToArgb());
mergedImage.SetPixel(x, y, mergedPixel);
}
}
mergedImage.Save("c:\\slika.jpg", ImageFormat.Jpeg);
MessageBox.Show(“Zavrseno");
}

}
}
}
ThumbnailImage & Icon
Image image = new Bitmap(@"c:\cym.jpg");
Image pThumbnail = image.GetThumbnailImage(100, 100,
null, new IntPtr());
g.DrawImage(pThumbnail, 10, 10, pThumbnail.Width,
pThumbnail.Height);

Icon icon = new Icon(@"c:\frd.ico", 64, 64);


g.DrawImage(icon.ToBitmap(), 50, 50);
ImageAttributes
Name Description
ClearBrushRemapTable Clears the brush color-remap table of this ImageAttributes object.

ClearColorKey() Clears the color key (transparency range) for the default category.

ClearColorKey(ColorAdjustType) Clears the color key (transparency range) for a specified category.

ClearColorMatrix() Clears the color-adjustment matrix for the default category.


ClearColorMatrix(ColorAdjustType) Clears the color-adjustment matrix for a specified category.

ClearGamma() Disables gamma correction for the default category.


ClearGamma(ColorAdjustType) Disables gamma correction for a specified category.

ClearNoOp() Clears theNoOp setting for the default category.


ClearNoOp(ColorAdjustType) Clears theNoOp setting for a specified category.
ClearOutputChannel() Clears the CMYK (cyan-magenta-yellow-black) output channel setting for the default
category.
ClearOutputChannel(ColorAdjustType) Clears the (cyan-magenta-yellow-black) output channel setting for a specified category.

ClearOutputChannelColorProfile() Clears the output channel color profile setting for the default category.

ClearOutputChannelColorProfile(ColorAdj Clears the output channel color profile setting for a specified category.
ustType)
ImageAttributes
Name Description
ClearRemapTable() Clears the color-remap table for the default category.
ClearRemapTable(ColorAdjustTy Clears the color-remap table for a specified category.
pe)
ClearThreshold() Clears the threshold value for the default category.
ClearThreshold(ColorAdjustType Clears the threshold value for a specified category.
)
Clone Creates an exact copy of thisImageAttributesobject.
GetAdjustedPalette Adjusts the colors in a palette according to the adjustment settings of a specified category.
SetBrushRemapTable Sets the color-remap table for the brush category.
SetColorKey(Color, Color) Sets the color key for the default category.
SetColorKey(Color, Color, Sets the color key (transparency range) for a specified category.
ColorAdjustType)
SetColorMatrices(ColorMatrix, Sets the color-adjustment matrix and the grayscale-adjustment matrix for the default category.
ColorMatrix)
SetColorMatrices(ColorMatrix, Sets the color-adjustment matrix and the grayscale-adjustment matrix for the default category.
ColorMatrix, ColorMatrixFlag)
SetColorMatrices(ColorMatrix, Sets the color-adjustment matrix and the grayscale-adjustment matrix for a specified category.
ColorMatrix, ColorMatrixFlag,
ColorAdjustType)
SetColorMatrix(ColorMatrix) Sets the color-adjustment matrix for the default category.
SetColorMatrix(ColorMatrix, Sets the color-adjustment matrix for the default category.
ColorMatrixFlag)
SetColorMatrix(ColorMatrix, Sets the color-adjustment matrix for a specified category.
ColorMatrixFlag,
ColorAdjustType)
SetGamma(Single) Sets the gamma value for the default category.
SetGamma(Single, Sets the gamma value for a specified category.
ColorAdjustType)
ImageAttributes
Name Description
SetNoOp() Turns off color adjustment for the default category. You can call theClearNoOpmethod to reinstate the color-adjustment settings that
were in place before the call to the SetNoOpmethod.
SetNoOp(ColorAdjustType) Turns off color adjustment for a specified category. You can call theClearNoOpmethod to reinstate the color-adjustment settings that
were in place before the call to the SetNoOpmethod.
SetOutputChannel(ColorChannelFlag Sets the CMYK (cyan-magenta-yellow-black) output channel for the default category.
)
SetOutputChannel(ColorChannelFlag Sets the CMYK (cyan-magenta-yellow-black) output channel for a specified category.
, ColorAdjustType)
SetOutputChannelColorProfile(String Sets the output channel color-profile file for the default category.
)
SetOutputChannelColorProfile(String Sets the output channel color-profile file for a specified category.
, ColorAdjustType)
SetRemapTable(ColorMap[]) Sets the color-remap table for the default category.
SetRemapTable(ColorMap[], Sets the color-remap table for a specified category.
ColorAdjustType)
SetThreshold(Single) Sets the threshold (transparency range) for the default category.
SetThreshold(Single, Sets the threshold (transparency range) for a specified category.
ColorAdjustType)
SetWrapMode(WrapMode) Sets the wrap mode that is used to decide how to tile a texture across a shape, or at shape boundaries. A texture is tiled across a
shape to fill it in when the texture is smaller than the shape it is filling.
SetWrapMode(WrapMode, Color) Sets the wrap mode and color used to decide how to tile a texture across a shape, or at shape boundaries. A texture is tiled across a
shape to fill it in when the texture is smaller than the shape it is filling.
SetWrapMode(WrapMode, Color, Sets the wrap mode and color used to decide how to tile a texture across a shape, or at shape boundaries. A texture is tiled across a
Boolean) shape to fill it in when the texture is smaller than the shape it is filling.
DrawImage & ImageAttributes
Image image = new Bitmap("InputColor.bmp");
ImageAttributes imageAttributes = new ImageAttributes();
int width = image.Width;
int height = image.Height;
float[][] colorMatrixElements = {
new float[] {2, 0, 0, 0, 0}, // red scaling factor of 2
new float[] {0, 1, 0, 0, 0}, // green scaling factor of 1
new float[] {0, 0, 1, 0, 0}, // blue scaling factor of 1
new float[] {0, 0, 0, 1, 0}, // alpha scaling factor of 1
new float[] {.2f, .2f, .2f, 0, 1}}; // three translations of 0.2
ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix( colorMatrix, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
g.DrawImage(image, 10, 10);
g.DrawImage( image, new Rectangle(120, 10, width, height), 0, 0, width, height,
GraphicsUnit.Pixel, imageAttributes);

You might also like