<p>This never writes anything to the output file because you haven't included any code to write to the output file. The only time you touch the file after opening it is in the line</p>

<pre><code>png-&gt;MakeFromFILE(pFile);
</code></pre>

<p>which <strong>reads</strong> from the file <strong>into</strong> the <code>SkData</code> instance. It won't even do that in this case, because the file handle has to be open for reading only.</p>

<p>I'm no expert on Skia, but you probably want to use <a href="https://skia-doc.commondatastorage.googleapis.com/doxygen/doxygen/html/classSkFILEWStream.html" rel="nofollow noreferrer"><code>SkFILEWStream</code></a> to write the file. Or just call <code>png.bytes()</code> and write the bytes to the file using the standard library.</p>

CC BY-SA 3.0
2017-04-04T12:42:12.333Z
2017-04-04T12:42:12.333Z
Dan Hulme
<p>I am just trying to make a simple Skia program. A very simple program, but as I run this code, the output image file never writes anything. I am running this code on VS 2015 and included the skia.lib and OpenGL32.lib static libraries.</p>

<p>Can someone tell me why this simple example does not work?</p>

<pre><code>#include "skiaIncludes.h"

void draw(SkCanvas* canvas) {
SkPaint paint;
paint.setColor(SK_ColorWHITE);
canvas-&gt;drawPaint(paint);
}

int main ()
{
try {
    FILE * pFile;
    char buffer[100];

    pFile = fopen("myfile.png", "wb");

sk_sp&lt;SkSurface&gt; rasterSurface(
        SkSurface::MakeRasterN32Premul(300, 300));
    SkCanvas* rasterCanvas = rasterSurface-&gt;getCanvas();

    //rasterCanvas-&gt;save();
    rasterCanvas-&gt;translate(SkIntToScalar(128), SkIntToScalar(128));
    rasterCanvas-&gt;rotate(SkIntToScalar(45));
    SkRect rect = SkRect::MakeXYWH(-90.5f, -90.5f, 181.0f, 181.0f);
    SkPaint paint;
    paint.setColor(SK_ColorBLUE);
    rasterCanvas-&gt;drawRect(rect, paint);
    //rasterCanvas-&gt;restore();

    sk_sp&lt;SkImage&gt; img = rasterSurface-&gt;makeImageSnapshot();
    sk_sp&lt;SkData&gt; png(img-&gt;encode(SkEncodedImageFormat::kPNG, 100));
    rasterCanvas-&gt;drawImage(img, 0, 0);
    png-&gt;MakeFromFILE(pFile);

    //sk_sp&lt;SkImage&gt; bmap = SkImage::MakeFromBitmap(bitmap);
    //SkData * encBmap = bmap-&gt;encode(SkEncodedImageFormat::kJPEG, 100);
    //encBmap-&gt;MakeFromFILE(pFile);

    fclose(pFile);
}
catch (std::exception ex){
    printf(ex.what());
}

return 0;
}
</code></pre>

<p>Header file :</p>

<pre><code>#pragma once


#include "core/SkData.h"
#include "core/SkImage.h"
#include "core/SkStream.h"
#include "core/SkSurface.h"
#include "core/SkCanvas.h"
#include "core/SkGraphics.h"
#include "core/SkImageEncoder.h"
#include "gpu/gl/GrGLInterface.h"
#include "gpu/gl/GrGLExtensions.h"
#include &lt;fstream&gt;
#include &lt;sys/stat.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;exception&gt;
#include &lt;iostream&gt;
//#include "gpu/gl/GrGLUtil.h"
</code></pre>

CC BY-SA 3.0
2017-04-01T08:17:51.377Z
Have you tried https://groups.google.com/forum/#!forum/skia-discuss? You're unlikely to get help on CGSE as the question isn't about directly computer graphics, but about using a specific API and file I/O. See http://computergraphics.stackexchange.com/help/on-topic
PeteUK
<c++><2d>
Skia example code not writing an image
user6371
# Skia example code not writing an image

(Asked by: username_0 on 2017-03-31)

I am just trying to make a simple Skia program. A very simple program, but as I run this code, the output image file never writes anything. I am running this code on VS 2015 and included the skia.lib and OpenGL32.lib static libraries.

Can someone tell me why this simple example does not work?

```
#include "skiaIncludes.h"

void draw(SkCanvas* canvas) {
SkPaint paint;
paint.setColor(SK_ColorWHITE);
canvas->drawPaint(paint);
}

int main ()
{
try {
    FILE * pFile;
    char buffer[100];

    pFile = fopen("myfile.png", "wb");

sk_sp<SkSurface> rasterSurface(
        SkSurface::MakeRasterN32Premul(300, 300));
    SkCanvas* rasterCanvas = rasterSurface->getCanvas();

    //rasterCanvas->save();
    rasterCanvas->translate(SkIntToScalar(128), SkIntToScalar(128));
    rasterCanvas->rotate(SkIntToScalar(45));
    SkRect rect = SkRect::MakeXYWH(-90.5f, -90.5f, 181.0f, 181.0f);
    SkPaint paint;
    paint.setColor(SK_ColorBLUE);
    rasterCanvas->drawRect(rect, paint);
    //rasterCanvas->restore();

    sk_sp<SkImage> img = rasterSurface->makeImageSnapshot();
    sk_sp<SkData> png(img->encode(SkEncodedImageFormat::kPNG, 100));
    rasterCanvas->drawImage(img, 0, 0);
    png->MakeFromFILE(pFile);

    //sk_sp<SkImage> bmap = SkImage::MakeFromBitmap(bitmap);
    //SkData * encBmap = bmap->encode(SkEncodedImageFormat::kJPEG, 100);
    //encBmap->MakeFromFILE(pFile);

    fclose(pFile);
}
catch (std::exception ex){
    printf(ex.what());
}

return 0;
}

```

Header file :

```
#pragma once

#include "core/SkData.h"
#include "core/SkImage.h"
#include "core/SkStream.h"
#include "core/SkSurface.h"
#include "core/SkCanvas.h"
#include "core/SkGraphics.h"
#include "core/SkImageEncoder.h"
#include "gpu/gl/GrGLInterface.h"
#include "gpu/gl/GrGLExtensions.h"
#include <fstream>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <exception>
#include <iostream>
//#include "gpu/gl/GrGLUtil.h"

```

**Tags:** c++, 2d

> **Comments:**
> **username_1**: Have you tried https://groups.google.com/forum/#!forum/skia-discuss? You're unlikely to get help on CGSE as the question isn't about directly computer graphics, but about using a specific API and file I/O. See http://computergraphics.stackexchange.com/help/on-topic  

---

## 1 answers

### Answer by username_2 on 2017-04-04. Score: 3

This never writes anything to the output file because you haven't included any code to write to the output file. The only time you touch the file after opening it is in the line

```
png->MakeFromFILE(pFile);

```

which **reads** from the file **into** the `SkData` instance. It won't even do that in this case, because the file handle has to be open for reading only.

I'm no expert on Skia, but you probably want to use [`SkFILEWStream`](https://skia-doc.commondatastorage.googleapis.com/doxygen/doxygen/html/classSkFILEWStream.html) to write the file. Or just call `png.bytes()` and write the bytes to the file using the standard library.


---

