Presentation is loading. Please wait.

Presentation is loading. Please wait.

Računarska grafika GDI+ (Graphics Device Interface Plus)

Similar presentations


Presentation on theme: "Računarska grafika GDI+ (Graphics Device Interface Plus)"— Presentation transcript:

1 Računarska grafika GDI+ (Graphics Device Interface Plus)

2 Introduction Windows GDI+ is the portion of the Windows XP operating system or Windows Server 2003 operating system that provides two-dimensional vector graphics, imaging, and typography. GDI+ improves on Windows Graphics Device Interface (GDI) (the graphics device interface included with earlier versions of Windows) by adding new features and by optimizing existing features.

3 The namespaces in GDI+ System.Drawing ▫This is the core GDI+ namespace. It defines objects for basic rendering (fonts, pens, basic brushes, etc.) and the most important object: Graphics. System.Drawing.Drawing2D ▫This gives you objects for more advanced two- dimensional vector graphics. Some of them are gradient brushes, pen caps, and geometric transforms.

4 The namespaces in GDI+ System.Drawing.Imaging ▫If you want to change graphical images - that is, change the palette, extract image metadata, manipulate metafiles, and so forth - this is the one you need. System.Drawing.Printing ▫To render images to the printed page, interact with the printer itself, and format the overall appearance of a print job, use the objects here.

5 The namespaces in GDI+ System.Drawing.Text ▫You can use collections of fonts with this namespace.

6 Graphics object The place to start with GDI+ is the Graphics object. Although the things you draw show up on your monitor or a printer, the Graphics object is the "canvas" that you draw on. Graphics object is always associated with a particular device context.

7 How do I get a Graphics object? You can get Graphics object from other objects. (e.g. event e parameter that is passed to the OnPaint event) You can use the CreateGraphicsmethod for a device context to create a Graphics object.

8 Example private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.DrawLine(new Pen(Color.Red), 0, 0, 250, 250); } -or- protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; g.DrawLine(new Pen(Color.Red), 0, 0, 250, 250); } -or- private void button1_Click(object sender, EventArgs e) { Graphics g = this.CreateGraphics(); g.DrawLine(new Pen(Color.Red), 0, 0, 250, 250); }

9

10 Vector Graphics versus Bitmaps "vector" is another word for a line, this way of using GDI+ is often called vector graphics. The other major type of graphics is create graphics using individual points of color like your TV or computer monitor does it. This is called bitmap graphics and will be covered in a later segment.

11

12 Coordinate spaces There are three distinct coordinate spaces in GDI+. These are: World coordinate space. This is where you put the coordinates that define lines, shapes and points in the 2 dimensional space of the graphics system. World coordinates are abstract values expressed as floating point numbers. Essentially, whenever you draw something it goes into this coordinate space. Page Coordinate Space. The Page space is where the world coordinates are transformed into some real-world value. You can make the Page Space represent pixels, inches millimeters and so-on. This is what makes GDI+ a resolution independent system. You control how the page space interprets the world space by telling the Graphics object what PageUnit is being used and adjusting the PageScale. Device Coordinate Space. This space is controlled by the system and enables the real-world values in the Page Space to be translated to your screen or printer. Device space ensures that a 1 inch long line looks an inch long on the screen and on the printer even though the two devices may have very different pixel resolutions. You have no direct control over this space.

13 Defining coordinate begining & orentations Graphics g = e.Graphics; GraphicsState StanjePre = g.Save(); g.PageUnit = GraphicsUnit.Pixel; Matrix matricaTransformacija = new Matrix(1, 0, 0, -1, 0, 0); matricaTransformacija.Translate(this.ClientRectangl e.Width / 2, -this.ClientRectangle.Height / 2); g.Transform = matricaTransformacija; g.DrawLine(new Pen(Color.FromArgb(255, 0, 0), 3), 0, 0, 100, 100); g.Restore(StanjePre); g.DrawLine(new Pen(Color.FromArgb(0, 255, 0), 3), 0, 0, 100, 100);

14 Color Color.FromArgb(255, 0, 0); Color.FromArgb(128, 255, 0, 0); Color.FromName("red"); Color.FromKnownColor(KnownColor.Red); Color.Red;

15 Pen NameDescription Pen(Brush) Initializes a new instance of the Pen class with the specified Brush.Brush Pen(Color) Initializes a new instance of thePen class with the specified color. Pen(Brush, Single) Initializes a new instance of the Pen class with the specified Brush and Width.BrushWidth Pen(Color, Single)Initializes a new instance of the Pen class with the specified Color and Width properties.ColorWidth

16 Pen // Create a new pen. Pen skyBluePen = new Pen(Brushes.DeepSkyBlue); // Set the pen's width. skyBluePen.Width = 8.0F; // Set the LineJoin property. skyBluePen.LineJoin = LineJoin.Bevel; // Draw a rectangle. e.Graphics.DrawRectangle(skyBluePen, new Rectangle(40, 40, 150, 200)); //Dispose of the pen. skyBluePen.Dispose();

17 LineCap – Start & End MemberDescription FlatSpecifies a flat line cap. SquareSpecifies a square line cap. RoundSpecifies a round line cap. TriangleSpecifies a triangular line cap. NoAnchorSpecifies no anchor. SquareAnchorSpecifies a square anchor line cap. RoundAnchorSpecifies a round anchor cap. DiamondAnchorSpecifies a diamond anchor cap. ArrowAnchorSpecifies an arrow-shaped anchor cap. CustomSpecifies a custom line cap. AnchorMaskSpecifies a mask used to check whether a line cap is an anchor cap. Olovka.StartCap = LineCap.DiamondAnchor; Olovka.EndCap = LineCap.Triangle; g.DrawLine(Olovka, 50, 500, 500, 500); Olovka.StartCap = LineCap.DiamondAnchor; Olovka.EndCap = LineCap.Triangle; g.DrawLine(Olovka, 50, 500, 500, 500);

18 LineJoin MemberDescription Miter Specifies a mitered join. This produces a sharp corner or a clipped corner, depending on whether the length of the miter exceeds the miter limit. Bevel Specifies a beveled join. This produces a diagonal corner. Round Specifies a circular join. This produces a smooth, circular arc between the lines. MiterClippedSpecifies a mitered join. This produces a sharp corner or a beveled corner, depending on whether the length of the miter exceeds the miter limit. Olovka.LineJoin = LineJoin.Bevel;

19 DashStyle MemberDescription SolidSpecifies a solid line. Dash Specifies a line consisting of dashes. Dot Specifies a line consisting of dots. DashDot Specifies a line consisting of a repeating pattern of dash-dot. DashDotDot Specifies a line consisting of a repeating pattern of dash-dot-dot. Custom Specifies a user-defined custom dash style. Pen Olovka = new Pen(Color.Black, 10); float[] NasaIsprekLinija = {5.0f, 2.0f, 1.0f, 3.0f}; Olovka.DashPattern = NasaIsprekLinija; Pen Olovka = new Pen(Color.Black, 10); float[] NasaIsprekLinija = {5.0f, 2.0f, 1.0f, 3.0f}; Olovka.DashPattern = NasaIsprekLinija; Olovka.DashStyle = DashStyle.DashDot;

20 Drawing basic shapes To draw lines with Windows GDI+ you need to create a Graphics object and a Pen object.GraphicsPen The Graphics object provides the methods that actually do the drawing, and the Pen object stores attributes of the line, such as color, width, and style.

21 Basic shapes Lines Rectangles Ellipses Arcs Pies Polygons Cardinal Splines Bézier Splines Paths Open and Closed Curves Regions Clipping

22 Lines public void DrawLine(Pen pen, Point pt1, Point pt2 ); Pen Type: System.Drawing.Pen Pen that determines the color, width, and style of the line.System.Drawing.Pen Pen Pt1 Type: System.Drawing.Point Point structure that represents the first point to connect. System.Drawing.Point Point Pt2 Type: System.Drawing.Point Point structure that represents the second point to connect. System.Drawing.Point Point

23 Lines g.DrawLine(new Pen(Color.Red, 3.2f), 0, 0, 250, 250); Point PocetnaTacka = new Point(0,0); Point KrajnjaTacka = new Point(250,50); Pen Olovka = new Pen(Color.GreenYellow, 3.2f); g.DrawLine(Olovka, PocetnaTacka, KrajnjaTacka);

24 Rectangles public void DrawRectangle( Pen pen, Rectangle rect ) Pen Type: System.Drawing.Pen A Pen that determines the color, width, and style of the rectangle. System.Drawing.PenPen Rect Type: System.Drawing.Rectangle A Rectangle structure that represents the rectangle to draw. System.Drawing.RectangleRectangle

25 Rectangles g.DrawRectangle(new Pen(Color.Blue, 2.5f), 50, 50, 150, 100); g.DrawRectangle(new Pen(Color.Blue, 2.5f), new Rectangle(50, 50, 100, 50));

26 Elipses public void DrawEllipse( Pen pen, Rectangle rect ) Pen Type: System.Drawing.Pen A Pen that determines the color, width, and style of the rectangle. System.Drawing.PenPen Rect Type: System.Drawing.Rectangle A Rectangle structure that represents the rectangle to draw. System.Drawing.RectangleRectangle

27 Ellipses g.DrawEllipse(new Pen(Color.Black, 3), 50, 50, 150, 100); g.DrawEllipse(new Pen(Color.Blue, 2.5f), new Rectangle(50, 50, 100, 50));

28 Arcs g.DrawArc(new Pen(Color.Black, 3), 50, 50, 150, 100, 0, 140);

29 Pies Graphics g = e.Graphics; Matrix matricaTransformacija = new Matrix(1, 0, 0, -1, 0, 0); matricaTransformacija.Translate(this.ClientRectangle.Width / 2, -this.ClientRectangle.Height / 2); g.Transform = matricaTransformacija; g.DrawLine(new Pen(Color.Black), -this.ClientRectangle.Width / 2, 0, this.ClientRectangle.Width / 2, 0); g.DrawLine(new Pen(Color.Black), 0, this.ClientRectangle.Height / 2, 0, - this.ClientRectangle.Height / 2); Pen Olovka = new Pen(Color.FromName("red"), 3); g.DrawPie(Olovka, -100, -100, 200, 200, 0, 300);

30 Pies

31 Polygons Point [] tacke = new Point[] { new Point(100, 25), new Point(25, 100), new Point(150, 200), new Point(275, 100), new Point(200, 25)}; Pen Olovka = new Pen(Color.Black, 3); g.DrawPolygon(Olovka, tacke);

32 Cardinal & Bézier Splines

33 Bézier Splines

34 Paths Paths are formed by combining lines, rectangles, and simple curves. ▫Lines ▫Rectangles ▫Ellipses ▫Arcs ▫Polygons ▫Cardinal splines ▫Bézier splines

35 Paths GraphicsPath gp = new GraphicsPath(FillMode.Alternate); //gp.AddPolygon(tacke); gp.AddLine(50, 150, 50, 350); gp.AddLine(50, 350, 250, 350); gp.AddArc(250, 250, 400, 200, 180, -180); gp.AddLine(650, 350, 850, 350); gp.AddLine(850, 350, 850, 150); gp.AddLine(850, 150, 650, 150); gp.Ad,dArc(250, 50, 400, 200, 0, -180); //gp.CloseFigure(); gp.AddLine(250, 150, 50, 150); gp.AddRectangle(new Rectangle(300, 200, 300, 100));

36 Paths g.FillPath(new SolidBrush(Color.Yellow), gp); g.DrawPath(Olovka, gp);

37 Brushes: To fill a closed shape, you need a Graphics object and a Brushobject.GraphicsBrush ▫Solid Brushes ▫Hatch Brushes ▫Texture Brushes ▫Gradient Brushes

38 Solid Brushes public SolidBrush( Color color ) color Type: System.Drawing.Color A Color structure that represents the color of this brush. System.Drawing.ColorColor

39 Solid Brushes g.Clear(Color.Black); SolidBrush CetkaR = new SolidBrush(Color.FromArgb(128, 255, 0, 0)); SolidBrush CetkaG = new SolidBrush(Color.FromArgb(128, 0, 255, 0)); SolidBrush CetkaB = new SolidBrush(Color.FromArgb(128, 0, 0, 255)); g.FillEllipse(CetkaR, 50, 50, 300, 300); g.FillEllipse(CetkaG, 250, 50, 300, 300); g.FillEllipse(CetkaB, 150, 200, 300, 300);

40 Hatch Brushes public HatchBrush( HatchStyle hatchstyle, Color foreColor, Color backColor ) hatchstyle Type: System.Drawing.Drawing2D.HatchStyle One of the HatchStyle values that represents the pattern drawn by this HatchBrush. System.Drawing.Drawing2D.HatchStyleHatchStyleHatchBrush foreColor Type: System.Drawing.Color The Color structure that represents the color of lines drawn by this HatchBrush. System.Drawing.ColorColorHatchBrush backColor Type: System.Drawing.Color The Color structure that represents the color of spaces between the lines drawn by this HatchBrush. System.Drawing.ColorColorHatchBrush

41

42 Hatch Brushes g.Clear(Color.White); HatchBrush Cetka = new HatchBrush(HatchStyle.Cross, Color.Green); HatchBrush Cetka1 = new HatchBrush(HatchStyle.Cross, Color.Black, Color.Red); HatchBrush Cetka2 = new HatchBrush(HatchStyle.Divot, Color.Yellow, Color.Blue); g.FillEllipse(Cetka, 50, 50, 300, 300); g.FillEllipse(Cetka1, 250, 50, 300, 300); g.FillEllipse(Cetka2, 150, 200, 300, 300);

43 Texture Brushes g.Clear(Color.White); Image slika = Image.FromFile("C:\\Documents and Settings\\Ognjen\\Desktop\\RG\\slike\\texture__.jpg"); Image slika1 = Image.FromFile("C:\\Documents and Settings\\Ognjen\\Desktop\\RG\\slike\\Y-YellowTexture.bmp"); Image slika2 = Image.FromFile("newwall.bmp"); TextureBrush Cetka = new TextureBrush(slika); TextureBrush Cetka1 = new TextureBrush(slika1); TextureBrush Cetka2 = new TextureBrush(slika2);


Download ppt "Računarska grafika GDI+ (Graphics Device Interface Plus)"

Similar presentations


Ads by Google