File: sec-draw-images.html

package info (click to toggle)
gtkmm-documentation 4.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,772 kB
  • sloc: cpp: 15,541; javascript: 1,208; makefile: 1,080; python: 401; xml: 106; perl: 67; sh: 8
file content (231 lines) | stat: -rw-r--r-- 9,052 bytes parent folder | download
1
2
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
      hljs.configure({languages: ['cpp']});
      hljs.highlightAll();
    </script><title>Drawing Images</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="chapter-drawingarea.html" title="Chapter 18. The DrawingArea Widget">
<link rel="prev" href="sec-drawing-text.html" title="Drawing Text">
<link rel="next" href="sec-drawing-clock-example.html" title="Example Application: Creating a Clock with Cairo">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Drawing Images</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-drawing-text.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 18. The DrawingArea Widget</th>
<td width="20%" align="right"> <a accesskey="n" href="sec-drawing-clock-example.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-draw-images"></a>Drawing Images</h2></div></div></div>
  
          <p>
              There is a method for drawing from a
              <code class="classname">Gdk::Pixbuf</code> to a <code class="classname">Cairo::Context</code>.
              A <code class="classname">Gdk::Pixbuf</code> buffer is a useful wrapper
              around a collection of pixels, which can be read from files, and
              manipulated in various ways.
          </p>
          <p>
              Probably the most common way of creating
              <code class="classname">Gdk::Pixbuf</code>s is to use
              <code class="methodname">Gdk::Pixbuf::create_from_file()</code> or
              <code class="methodname">Gdk::Pixbuf::create_from_resource()</code>,
              which can read an image file, such as a png file into a pixbuf
              ready for rendering.
          </p>
          <p>
              The <code class="classname">Gdk::Pixbuf</code> can be rendered by setting
              it as the source pattern of the Cairo context with
              <code class="methodname">Gdk::Cairo::set_source_pixbuf()</code>.
              Then draw the image with either <code class="methodname">Cairo::Context::paint()</code>
              (to draw the whole image), or <code class="methodname">Cairo::Context::rectangle()</code>
              and <code class="methodname">Cairo::Context::fill()</code> (to fill the
              specified rectangle). <code class="methodname">set_source_pixbuf()</code>
              is not a member of <code class="classname">Cairo::Context</code>. It takes
              a <code class="classname">Cairo::Context</code> as its first parameter.
          </p>
          <p>
              Here is a small bit of code to tie it all together: (Note that
              usually you wouldn't load the image every time in the draw
              signal handler! It's just shown here to keep it all together.)
          </p>
          <pre class="programlisting"><code class="code">void MyArea::on_draw(const Cairo::RefPtr&lt;Cairo::Context&gt;&amp; cr, int width, int height)
{
  auto image = Gdk::Pixbuf::create_from_file("myimage.png");
  // Draw the image at 110, 90, except for the outermost 10 pixels.
  Gdk::Cairo::set_source_pixbuf(cr, image, 100, 80);
  cr-&gt;rectangle(110, 90, image-&gt;get_width()-20, image-&gt;get_height()-20);
  cr-&gt;fill();
}</code></pre>

        <div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="cairo-example-image"></a>Example</h3></div></div></div>
        
            <p>
                Here is an example of a simple program that draws an image.
                The program loads the image from a resource file. See the <a class="link" href="sec-gio-resource.html" title="Gio::Resource and glib-compile-resources">Gio::Resource and glib-compile-resources</a>
                section. Use <span class="application">glib-compile-resources</span> to compile
                the resources into a C source file that can be compiled and
                linked with the C++ code. E.g.
                </p>
<pre class="screen">$ glib-compile-resources --target=resources.c --generate-source image.gresource.xml</pre>
<p>
            </p>
        <div class="figure">
<a name="figure-drawingarea-image"></a><p class="title"><b>Figure 18.7. Drawing Area - Image</b></p>
<div class="figure-contents">
          
            <div class="screenshot">
                <div class="mediaobject"><img src="figures/drawingarea_image.png" alt="Drawing Area - Image"></div>
            </div>
        </div>
</div>
<br class="figure-break">

<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/drawingarea/image" target="_top">Source Code</a></p>

<p>File: <code class="filename">myarea.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLE_MYAREA_H
#define GTKMM_EXAMPLE_MYAREA_H

#include &lt;gtkmm/drawingarea.h&gt;
#include &lt;gdkmm/pixbuf.h&gt;

class MyArea : public Gtk::DrawingArea
{
public:
  MyArea();
  virtual ~MyArea();

protected:
  void on_draw(const Cairo::RefPtr&lt;Cairo::Context&gt;&amp; cr, int width, int height);

  Glib::RefPtr&lt;Gdk::Pixbuf&gt; m_image;
};

#endif // GTKMM_EXAMPLE_MYAREA_H
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "myarea.h"
#include &lt;gtkmm/application.h&gt;
#include &lt;gtkmm/window.h&gt;

class ExampleWindow : public Gtk::Window
{
public:
  ExampleWindow();

protected:
  MyArea m_area;
};

ExampleWindow::ExampleWindow()
{
  set_title("DrawingArea");
  set_default_size(300, 200);
  set_child(m_area);
}

int main(int argc, char** argv)
{
  auto app = Gtk::Application::create("org.gtkmm.example");

  return app-&gt;make_window_and_run&lt;ExampleWindow&gt;(argc, argv);
}
</code></pre>
<p>File: <code class="filename">myarea.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "myarea.h"
#include &lt;cairomm/context.h&gt;
#include &lt;giomm/resource.h&gt;
#include &lt;gdkmm/general.h&gt; // set_source_pixbuf()
#include &lt;glibmm/fileutils.h&gt;
#include &lt;iostream&gt;

MyArea::MyArea()
{
  try
  {
    // The fractal image has been created by the XaoS program.
    // http://xaos.sourceforge.net
    m_image = Gdk::Pixbuf::create_from_resource("/image/fractal_image.png");
  }
  catch(const Gio::ResourceError&amp; ex)
  {
    std::cerr &lt;&lt; "ResourceError: " &lt;&lt; ex.what() &lt;&lt; std::endl;
  }
  catch(const Gdk::PixbufError&amp; ex)
  {
    std::cerr &lt;&lt; "PixbufError: " &lt;&lt; ex.what() &lt;&lt; std::endl;
  }

  // Show at least a quarter of the image.
  if (m_image)
  {
    set_content_width(m_image-&gt;get_width()/2);
    set_content_height(m_image-&gt;get_height()/2);
  }

  // Set the draw function.
  set_draw_func(sigc::mem_fun(*this, &amp;MyArea::on_draw));
}

MyArea::~MyArea()
{
}

void MyArea::on_draw(const Cairo::RefPtr&lt;Cairo::Context&gt;&amp; cr, int width, int height)
{
  if (!m_image)
    return;

  // Draw the image in the middle of the drawing area, or (if the image is
  // larger than the drawing area) draw the middle part of the image.
  Gdk::Cairo::set_source_pixbuf(cr, m_image,
    (width - m_image-&gt;get_width())/2, (height - m_image-&gt;get_height())/2);
  cr-&gt;paint();
}
</code></pre>
<p>File: <code class="filename">image.gresource.xml</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;gresources&gt;
  &lt;gresource prefix="/image"&gt;
    &lt;file&gt;fractal_image.png&lt;/file&gt;
  &lt;/gresource&gt;
&lt;/gresources&gt;
</code></pre>

        </div>
  </div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-drawing-text.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-drawingarea.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="sec-drawing-clock-example.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Drawing Text </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> Example Application: Creating a Clock with Cairo</td>
</tr>
</table>
</div>
</body>
</html>