Click here to Skip to main content
13,189,506 members (29,437 online)
Click here to Skip to main content


62 bookmarked
Posted 7 Dec 2009

Another Screensaver with WPF

, 27 Jan 2012
Lessons learnt from writing a screensaver with WPF
using System;
using System.Diagnostics;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using RZWScreenSaver.Graphics.ColorSpaces;

namespace RZWScreenSaver.Graphics{
    public class RawBitmap{
        public RawBitmap(BitmapSource bitmap){
            if (bitmap.Format != PixelFormats.Pbgra32)
                throw new NotSupportedException("Current supported pixel format is Pbgra32");

            var bytesPerPixel = (bitmap.Format.BitsPerPixel + 7) / 8;
            stride = bitmap.PixelWidth * bytesPerPixel;

            var length = bitmap.PixelHeight*stride;
            data = new byte[length];
            bitmap.CopyPixels(data, stride, 0);

            width = bitmap.PixelWidth;
            height = bitmap.PixelHeight;
            dpiX = bitmap.DpiX;
            dpiY = bitmap.DpiY;
            format = bitmap.Format;
            palette = bitmap.Palette;
        public byte[] Data{
            get { return data; }
        public int Stride{
            get { return stride; }
        public int Height{
            get { return height; }
        public int Width{
            get { return width; }
        public RawBitmap CloneWithData(Rgba32 rgbData){
            Debug.Assert(format == PixelFormats.Pbgra32);
            Debug.Assert(rgbData.Width == width && rgbData.Height == height);
            var result = (RawBitmap) MemberwiseClone();
   = rgbData.Data;
            return result;
        public BitmapSource ToBitmap(){
            var result = BitmapSource.Create(width, height, dpiX, dpiY, format, palette, data, stride);
            return result;
        byte[] data;
        readonly int stride;
        readonly int width, height;
        readonly double dpiX, dpiY;
        readonly PixelFormat format;
        readonly BitmapPalette palette;

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


About the Author

Ruxo Zheng
Technical Lead
Thailand Thailand
C/C++ and C# programmer.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171016.2 | Last Updated 27 Jan 2012
Article Copyright 2009 by Ruxo Zheng
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid