Skip to content

Instantly share code, notes, and snippets.

@zester
Created August 29, 2011 03:46
Show Gist options
  • Save zester/1177738 to your computer and use it in GitHub Desktop.
Save zester/1177738 to your computer and use it in GitHub Desktop.
Skia Graphics Library Example (Note: Use to work)
#include "SkCanvas.h"
#include "SkGraphics.h"
#include "SkImageEncoder.h"
#include "SkString.h"
#include "SkTemplates.h"
int main (int argc, char * const argv[]) {
//
SkAutoGraphics ag;
//Output filename
SkString path("skhello.png");
//Set Text To Draw
SkString text("Hello, World");
SkPaint paint;
//Set Text ARGB Color
paint.setARGB(255, 255, 255, 255);
//Turn AntiAliasing On
paint.setAntiAlias(true);
//Set Text Size
paint.setTextSize(SkIntToScalar(30));
//Set Image Width & Height
SkScalar width = 800;
SkScalar height = 600;
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
bitmap.allocPixels();
//Create Canvas
SkCanvas canvas(bitmap);
canvas.drawARGB(255, 101, 33, 131);
//Text X, Y Position Varibles
SkScalar x = 80;
SkScalar y = 60;
canvas.drawText(text.c_str(), text.size(), x, y, paint);
//Set Style and Stroke Width
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(10);
//Draw A Rectangle
SkRect rect;
paint.setARGB(255, 0, 0, 0);
//Left, Top, Right, Bottom
rect.set(50, 100, 200, 200);
canvas.drawRoundRect(rect, 20, 20, paint);
//Draw A Line
canvas.drawLine(10, 300, 300, 300, paint);
//Draw Circle (X, Y, Size, Paint)
canvas.drawCircle(100, 400, 50, paint);
//Same Image (file, bitmap, image_type, quality)
SkImageEncoder::EncodeFile(path.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
return 0;
}
@javedulu
Copy link

SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
bitmap.allocPixels();

needs to be changed to :

SkBitmap bitmap;
bitmap.allocPixels(SkImageInfo::MakeN32Premul(width,height));

@peter279k
Copy link

Unfortunately, this code is failed during the compilation.

I use the latest skia version and try to compile this sample code.But it's failed.

The error message is as follows:

main.cpp: In function ‘int main(int, char* const*)’:
main.cpp:65:5: error: ‘SkImageEncoder’ has not been declared
     SkImageEncoder::EncodeFile(path.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
     ^
main.cpp:65:54: error: ‘SkImageEncoder’ has not been declared
     SkImageEncoder::EncodeFile(path.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
                                                      ^

How could I fix this problem? Thanks.

BTW, the drawARGB method of SkCanvas class is deprecated or unavailable now.
You should change it to the drawColor method...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment