Object Oriented Programming

Slides:



Advertisements
Similar presentations
Computer Graphics Prof. Muhammad Saeed Dept. of Computer Science & IT Federal Urdu University of Arts, Sciences and Technology.
Advertisements

2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Nov 2005 MSc Slide 1 - Another Example of a Structural Pattern Alternative way of adding Functionality to an existing class (alternative to a derived class)
 2007 Dr. Natheer Khasawneh. Chapter 13. Graphical User Interface Concepts: Part 1.
1 USING FIREWORKS Cropping an Image Obtaining Information Changing Image Size Rotating an Image Adjusting the Color of an Image Drawing Tools Using the.
Macromedia Director 8 Foundation Level Course. What is Director? Director is a challenging program for creating animation and multimedia productions for.
Graphics and Multimedia Session 13 Mata kuliah: M0874 – Programming II Tahun: 2010.
User Interface Programming in C#: Graphics
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Graphics and Multimedia. Outline Introduction Graphics Contexts and Graphics Objects Color Control.
Graphics and Multimedia. Outline Drawing Polygons and Polylines Advanced Graphics Capabilities.
Chapter 13: Advanced GUI and Graphics
InDesign CS3 Lesson 3 Working with Frames. Using Frames Frames are containers in which you place graphics or text. Frames can also be used as graphic.
Graphics and Multimedia. Introduction The language contains many sophisticated drawing capabilities as part of namespace System.Drawing and the other.
1 Chapter 26 D&D – Graphics Outline 26.1 Introduction 26.3 Graphics Contexts and Graphics Objects 26.4 Color Control 26.5 Font Control 26.6 Drawing Lines,
By: Zaiba Mustafa Copyright ©
Graphics and Multimedia In visual Studio. Net (C#)
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface A Brief Introduction to GDI+ S.R.G. Fraser, Pro Visual C++/CLI.
BUILDING RICH MEDIA ELEMENTS. Design Decisions Based on Design Specification  Following the design specification will ensure that the application is.
CST238 Week 5 Questions / Concerns? Announcements – HW#1 due (Project ideas) – Check-off Take Home lab#4 Recap New topics – Drawing Coming up: – GUI Bloopers.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 26 – CheckWriter Application Introducing Graphics.
1 Windows Graphics. 2 Objectives You will be able to Use the Windows GDI+ to draw arbitrary figures and text on a Windows form. Add a handler for the.
Object Oriented Programming Graphical User Interfaces Dr. Mike Spann
Lecture 8 Building an MDI Application. Introduction The MDI (Multiple Document Interface) provides a way to display multiple (child) windows forms inside.
Tutorial 2 Drawing Text, Adding Shapes, and Creating Symbols.
Object Oriented Programming Graphics and Multimedia Dr. Mike Spann
C# Programming Lecture 4 “GDI+” PGL01/CSP/2006.
CSC 298 Windows Forms.
Graphics and Multimedia Part #2
Visual Basic .NET BASICS
Developing the Game User Interface (UI) Lesson 5.
ImageJ EE4H, M.Sc Computer Vision Dr. Mike Spann
1 Scroll Bars Providing Scrollbars. 2 Objectives You will be able to: Use Windows Graphics to display tabular information on a Windows form. Add graphics.
Chapter 11 Bitmaps, Icons and Cursors. Copyright 2006 © Thomas P. Skinner2 Background Bitmaps are easy in.NET. Two kinds of graphics: 1.Vector 2.Bitmaps.
Visual C# 2012 How to Program 1. 2  A graphical user interface (GUI) allows a user to interact visually with a program.  Figure 14.1 shows a Visual.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
GDI+ 1. Objectives 2 GDI+ class  Create and render Graphic  Display information on the computer screen, printer 3.
Tutorial 2 Drawing Shapes, Adding Text, and Creating Symbols.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Introduction to Flash Animation CS 318. Topics Introduction to Flash and animation The Flash development environment Creating Flash animations  Layers.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
1 Graphic Device Interface (GDI). 2 Class Form A Form is a representation of any window displayed in your application. The Form class can be used to create.
12/5/2015 EEC492/693/793 - iPhone Application Development 1 EEC-693/793 Applied Computer Vision with Depth Cameras Lecture 4 Wenbing Zhao
16.9 Introduction to Multimedia Visual Basic offers many convenient ways to include images and animations in programs Computing field decades ago mainly.
Windows Programming C# Software Development. Overview  DLLs / PInvoke  DirectX .NET Component Library  Custom Controls  Event Model (in C++)  GUI.
2/16/2016 EEC492/693/793 - iPhone Application Development 1 EEC-693/793 Applied Computer Vision with Depth Cameras Lecture 4 Wenbing Zhao
GDI +. Graphics class's methods System.Drawing Graphics Objects.
Creating cast members  Using tool palette window that can be selected from window menu.  The following objects can be created: Push buttons, Radio buttons,
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Introducing Macromedia Flash 8
Working with Data Blocks and Frames
Getting Started with Adobe Photoshop CS6
5/23/2018 Additional Features of WordPad
Object Oriented Programming
Flash Interface, Commands and Functions
Introduction to Basic Interface of
Graphics and Multimedia
.NET and .NET Core 7. XAML Pan Wuming 2017.
ThS. Nguyễn Hà Giang Khoa CNTT - Hutech
CASE Tools Graphical User Interface Programming Using C#
Copyright © 2006 Thomas P. Skinner
DREAMWEAVER MX 2004 Chapter 3 Working with Tables
Windows Tutorial 7 Managing Multimedia Files
Lecture 9 GUI and Event Driven CSE /16/2019.
Develop Rich Internet Content and Applications
Advanced GUIs and Graphics
CPT 450 Computer Graphics 3rd Lecture.
Presentation transcript:

Object Oriented Programming Graphics and Multimedia Dr. Mike Spann m.spann@bham.ac.uk

Contents Introduction Drawing simple graphics Colour control an colour palettes Displaying and processing images Windows Media Player Summary

Introduction C# has extensive support for graphics and multimedia The FCL has sophisticated drawing capabilities within the System.Drawing namespace Selection of colours and fonts Drawing shapes and text Also as we have seen, the FCL contains an Image class allowing image processing applications Multimedia capabilities are provided by the Windows Media Player control which allow applications to play video and audio

Drawing simple graphics Object of class Graphics control the drawing of graphics in a C# application They set the graphics context Colours Fonts Linestyles They also contain methods for drawing graphics Simple shapes Text Rendering images

Drawing simple graphics Its important to realise that drawing graphics is an event driven process All derived classes of the Form class inherit an OnPaint() method This method is called when a component must be redrawn in response to a paint event The method is overrided in user classes to draw the required graphics protected override void OnPaint(PaintEventArgs e)

Drawing simple graphics Alternatively an event handler can be written to handle the paint event These methods are normally called automatically when a form needs to be repainted, for example when the form is covered/uncovered or moved It is also called when the form’s Invalidate() method is called which causes it to be refreshed protected void MyForm_Paint(object sender, PaintEventArgs e)

Drawing simple graphics Drawing simple graphics involves calling the relevant method of a Graphics object to draw the desired shape in the Paint event handler A SolidBrush object is created fill a region Pen objects are used to draw lines Other more sophisticated objects can fill regions with textured patterns

Drawing simple graphics public partial class ColourDemoForm : Form { public ColourDemoForm() InitializeComponent(); } protected override void OnPaint(PaintEventArgs e) Graphics graphics = e.Graphics; // Display rectangle SolidBrush brush = new SolidBrush(Color.Orange); graphics.FillRectangle(brush, 10, 10, 100, 100); // Display descriptive text SolidBrush textBrush = new SolidBrush(Color.BlueViolet); graphics.DrawString("Demonstrating simple graphics and colour", this.Font, textBrush, 10, 150);

Drawing simple graphics

Drawing simple graphics We can easily change the type and style of the font and add new shapes Fonts are creates with the Font class The type, style and fontsize are passed to the constructor We can draw filled shapes with a SolidBrush object and open shapes with a Pen object

Drawing simple graphics

Drawing simple graphics public partial class ColourDemoForm : Form { public ColourDemoForm() InitializeComponent(); } protected override void OnPaint(PaintEventArgs e) Graphics graphics = e.Graphics; // Display rectangle SolidBrush brush = new SolidBrush(Color.Orange); graphics.FillRectangle(brush, 10, 10, 100, 100); Pen pen = new Pen(Color.DarkTurquoise,4); graphics.DrawEllipse(pen, new Rectangle(150, 150, 150, 100)); // Display descriptive text FontStyle style = FontStyle.Bold|FontStyle.Italic; Font arial = new Font( "Arial" , 14, style ); SolidBrush textBrush = new SolidBrush(Color.BlueViolet); graphics.DrawString("Demonstrating simple graphics and colour", arial, textBrush, 10, 300);

Drawing simple graphics

Drawing simple graphics We can create more interactive graphical application which are under mouse control For example we can interactively specify the vertices of a polygon and use the Graphics.DrawPolygon() method to draw the polygon through the vertices We can initialize our vertices in a MouseDown() event handler Such an application could be the basis of an interactive drawing application

Drawing simple graphics

Drawing simple graphics It is simple to create our GUI for our drawing application using design view We have added 2 radio buttons to select either an open or closed polygon Adding any number of radio buttons to a container enforces only one of them to be selected Check boxes can have multiple selections

Drawing simple graphics public partial class PolygonForm : Form { public PolygonForm() InitializeComponent(); } private void drawPanel_MouseDown(object sender, MouseEventArgs e) private void drawPanel_Paint(object sender, PaintEventArgs e) private void clearButton_Click(object sender, EventArgs e) private void openPolygonSelectButton_CheckedChanged(object sender, EventArgs e) private void closedPolygonSelectButton_CheckedChanged(object sender, EventArgs e)

Drawing simple graphics Writing the code for the event handlers is straightforward The Paint event handlers draws the polygon as points are added to an ArrayList structure We draw the polygons on a separate Panel object The Paint event handler is called using the Invalidate() method

Drawing simple graphics public partial class PolygonForm : Form { private ArrayList vertices= new ArrayList(); private bool openPolygon = true; private Pen pen = new Pen(Color.Red); private SolidBrush brush = new SolidBrush(Color.Yellow); public PolygonForm() InitializeComponent(); } private void drawPanel_MouseDown(object sender, MouseEventArgs e) vertices.Add(new Point(e.X, e.Y)); drawPanel.Invalidate(); private void drawPanel_Paint(object sender, PaintEventArgs e) { // Paint event handler } private void clearButton_Click(object sender, EventArgs e) vertices.Clear(); drawPanel.Invalidate(); private void openPolygonSelectButton_CheckedChanged(object sender, EventArgs e) openPolygon = true; drawPanel.Invalidate(); private void closedPolygonSelectButton_CheckedChanged(object sender, EventArgs e) openPolygon = false; drawPanel.Invalidate();

Drawing simple graphics The Paint event handler calls the relevant method to draw an open or filled polygon The main work involved is converting the ArrayList object to a Point array which can be done using the ArrayList.ToArray method private void drawPanel_Paint(object sender, PaintEventArgs e) { Graphics graphics = e.Graphics; if (vertices.Count > 1) Point[] verticesArray=(Point[])(vertices.ToArray(vertices[0].GetType())); if (openPolygon) graphics.DrawPolygon(pen, verticesArray); else graphics.FillPolygon(brush, verticesArray); }

Drawing simple graphics Demos\Draw Polygons\DrawPolygon.exe

Colour control and colour palettes Colour is handled using a class Color An ARGB representation is used Each channel is 0-255 (A)lpha channel is the opacity RGB channels are the red, blue and green components The larger the value, the greater the amount of colour Color defines a number of static constants for pre-defined colours

Colour control and colour palettes

Colour control and colour palettes The alpha channel in the ARGB representation represents the opacity of the colour The 0 is transparant 255 is opaque We can blend two colours using intermediate alpha values

Colour control and colour palettes The ColorDialog class is a powerful feature enabling interactive selection of colours from a palette Can be used in many graphical applications The user can create custom colours from the palette, not just pre-selected ones

Colour control and colour palettes

Colour control and colour palettes public partial class ColourPaletteDemoForm : Form { private static ColorDialog colorChooser = new ColorDialog(); public ColourPaletteDemoForm() InitializeComponent(); } private void textColourButton_Click(object sender, EventArgs e) DialogResult result = colorChooser.ShowDialog(); if (result==DialogResult.Cancel) return; messageLabel.ForeColor = colorChooser.Color; private void BackgroundColourButton_Click(object sender, EventArgs e) colorChooser.FullOpen = true; if (result == DialogResult.Cancel) this.BackColor = colorChooser.Color;

Colour control and colour palettes Demos\Colour Palette\ColourPaletteDemo.exe

Displaying and processing images C# contains extensive support for image manipulation We have already seen how we can develop a simple application to load and display an image from file This application used the Image class The Graphics.DrawImage() method displayed the image Often applications require access to the individual (RGB) pixels of an image in order to manipulate the image in some way

Displaying and processing images Objects of class Bitmap hold images and allow the image pixels to be accessed Most Image methods apply to Bitmaps FromFile(), Save() etc In addition there are getPixel() and setPixel() methods for image access A bitmap object can easily be created from an image object Bitmap bitmap = new Bitmap(image);

Displaying and processing images For example, we can add an extra menu item to our image viewer tool to process images loaded from file Sub-menu items can include inverting the colours in an image red->255-red green->255-green blue->255-blue We thus need to directly access rgb pixel values

public partial class ImageForm : Form { private Image image; private Bitmap bitmap; public ImageForm(Image im) image = im; bitmap = new Bitmap(image); InitializeComponent(); } private void ImageForm_Paint(object sender, PaintEventArgs e) Graphics graphics = e.Graphics; graphics.DrawImage(bitmap, new Point(20,20)); public void invertImage() for (int x = 0; x < bitmap.Width; x++) for (int y = 0; y < bitmap.Height; y++) Color rgb=bitmap.GetPixel(x,y); Color invrgb = Color.FromArgb(255 - rgb.R, 255 - rgb.G, 255 - rgb.B); bitmap.SetPixel(x, y, invrgb); Invalidate();

Displaying and processing images Demos\Image Processor\Image Viewer.exe

Displaying and processing images For efficient image processing applications, including real time imaging applications, C# provides an unsafe mode allowing rapid image access Code is run outside the normal managed block avoiding the overheads of CLR runtime safety checks Allows the use of pointers!

Displaying and processing images The Bitmap class provides the Bitmap.LockBits() and corresponding Bitmap.UnlockBits() methods which enable you to fix a portion of the bitmap pixel data array in memory Allows direct access to pixel data enabling the data to be modified and written back into the bitmap LockBits() returns a BitmapData object that describes the layout and position of the data in the locked array

Displaying and processing images The BitmapData class contains the following important properties Scan0 - The address in memory of the fixed data array Stride - The width, in bytes, of a single row of pixel data. Usually data is packed into rows that begin on a four byte boundary and are padded out to a multiple of four bytes so the stride doesn’t equal the image width PixelFormat -The actual pixel format of the data. This is important for finding the right bytes Width - The width of the locked image Height - The height of the locked image

Displaying and processing images Unused Width Scan0 Height Stride

Displaying and processing images public void invertImage() { BitmapData data = bitmap.LockBits(new Rectangle(0, 0,bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); unsafe byte* imgPtr = (byte*)(data.Scan0); for (int i = 0; i < data.Height; i++) for (int j = 0; j < data.Width; j++) (*imgPtr) = (byte)( 255 - (*imgPtr++)); //R (*imgPtr) = (byte) (255 - (*imgPtr++)); //G (*imgPtr) =(byte)( 255 - (*imgPtr++)); //B } imgPtr += data.Stride - data.Width * 3; bitmap.UnlockBits(data); Invalidate();

Windows Media Player The Windows Media Player control allows a user to create applications which can play video and audio It enables the use of many different types of media formats MPEG AVI WAV MIDI The control has a user interface containing buttons to control the playing of the media object

Windows Media Player public partial class MediaPlayerForm : Form { public MediaPlayerForm() InitializeComponent(); } private void openToolStripMenuItem_Click(object sender, EventArgs e) openMediaFileDialog.ShowDialog(); mediaPlayer.URL = openMediaFileDialog.FileName; // mediaPlayer.openPlayer(mediaPlayer.URL); // For normal GUI private void exitToolStripMenuItem_Click(object sender, EventArgs e) Application.Exit();

Windows Media Player The name of the media item to play is set using the URL property of the media player object (of class AxWindowsMediaPlayer) The media item (video) is played inside the bounds of the Form in which the media player control is embedded Demos\Media Player Embedded\MediaPlayer.exe We can also get the normal media player GUI by calling the openPlayer() method Demos\Media Player\MediaPlayer.exe

Summary We have seen how we can draw simple graphics such as basic shapes and text We have looked at how graphics is drawn in an event handler for the Paint event We have seen how colour is displayed using the alpha, red, green and blue channel and how we can select our own colour palette inside an application We have seen how we can display and process images We have seen how we can embed the Windows Media Player into an application to display multimedia objects such as video