Quantcast
Channel: WriteableBitmapEx
Viewing all 360 articles
Browse latest View live

Closed Issue: Color incorrect on loaded bitmap [19360]

$
0
0
I loaded a png file (4096x2800) and display it in an image control but the colors are incorrect; Blue's appear as brown etc.

I have loaded the same png file into a BitmapImage and then display in the image control and the color is correct.

I've used the latest downloadable binaries and compiled the latest sourcecode but the results are the same.

Visual Studio 2012 on Windows 8 project for WinRT
Comments: Just tried it with your Normal.png image using the below snippet where Normal.png is part of the project as Build Action = Content.
Works fine.

...
var imageUri = new Uri(BaseUri, "///Normal.png");
var bmp = await BitmapFactory.New(1, 1).FromContent(imageUri);
Img.Source = bmp;
...

Released: WriteableBitmapEx 1.0.8 (Oct 24, 2012)

$
0
0
This is the final version 1.0 of WriteableBitmapEx with Windows Phone 8 & 7, WPF, WinRT Windows Store XAML and Silverlight support
  • Added Windows Phone 8 support and sample
  • Much improved performance for WinRT XAML
  • Bug fixes for alpha blitting, rectangle drawing coordinates and much more

Contains the WriteableBitmapEx binaries for Windows Phone 8 & 7, WPF, WinRT Windows Store XAML and Silverlight. Download the source for the samples.

Updated Release: WriteableBitmapEx 1.0.8 (Oct 24, 2012)

$
0
0
This is the final version 1.0 of WriteableBitmapEx with Windows Phone 8 & 7, WPF, WinRT Windows Store XAML and Silverlight support
  • Added Windows Phone 8 support and sample
  • Much improved performance for WinRT XAML
  • Bug fixes for alpha blitting, rectangle drawing coordinates and much more

Contains the WriteableBitmapEx binaries for Windows Phone 8 & 7, WPF, WinRT Windows Store XAML and Silverlight. Download the source for the samples.

New Post: RPC_E_WRONG_THREAD Thrown When I Try to Create New WriteableBitmap.

$
0
0
Hi guys, I'm developing a game using WaveEngine, and I want to share an image through Facebook API, but when I try to create a new WriteableBitmap using the extension method .FromStream()...it throws the exception:
The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
Here's my snippet:

_wbmp is a WriteableBitmap object...
_wbmp = await new WriteableBitmap(1, 1).FromStream(_fileStream, BitmapPixelFormat.Unknown);
I already tried to use CoreDispatcher but it keeps throwing the same exception.

Anyone of you've face this before?

Thanks in advance.

New Post: RPC_E_WRONG_THREAD Thrown When I Try to Create New WriteableBitmap.

$
0
0
Makes sure you create a WriteableBitmap always on the UI thread. For that you should ensure the Dispatcher reference you use is instantiated in the UI thread.

New Post: RPC_E_WRONG_THREAD Thrown When I Try to Create New WriteableBitmap.

$
0
0
How can I know if the dispatcher was created in the UI thread?

New Post: save PNG (transparent picture)

$
0
0
To fix the pixel shift or discoloration issue search for
               data[dataOffset + 0] = pixels[pixelOffset + 0];
                data[dataOffset + 1] = pixels[pixelOffset + 1];
                data[dataOffset + 2] = pixels[pixelOffset + 2];
                data[dataOffset + 3] = pixels[pixelOffset + 3];

                if (y > 0)
                {
                    int lastOffset = ((y - 1) * _image.PixelWidth + x) * 4;

                    data[dataOffset + 0] -= pixels[lastOffset + 0];
                    data[dataOffset + 1] -= pixels[lastOffset + 1];
                    data[dataOffset + 2] -= pixels[lastOffset + 2];
                    data[dataOffset + 3] -= pixels[lastOffset + 3];
                }
and replace with
               data[dataOffset + 0] = pixels[pixelOffset + 2];
                data[dataOffset + 1] = pixels[pixelOffset + 1];
                data[dataOffset + 2] = pixels[pixelOffset + 0];
                data[dataOffset + 3] = pixels[pixelOffset + 3];

                if (y > 0)
                {
                    int lastOffset = ((y - 1) * _image.PixelWidth + x) * 4;

                    data[dataOffset + 0] -= pixels[lastOffset + 2];
                    data[dataOffset + 1] -= pixels[lastOffset + 1];
                    data[dataOffset + 2] -= pixels[lastOffset + 0];
                    data[dataOffset + 3] -= pixels[lastOffset + 3];
                }

New Post: Library doesn't work properly

$
0
0
Hi everyone,

I'm Italian, so sorry for my bad English.

I'm trying to use WriteableBitmapEx: I added it to my WPF project using NuGet (and downloading it and adding the reference to the project too), but it doesn't work.
First of all, I followed the tutorial on the homepage of the project (the sample of code), but I can't found the overload which requires only two arguments (writeableBmp = new WriteableBitmap(0, 0).FromResource("Data/flower2.png");) because Visual Studio shows me only two overloads: one that requires a BitmapElement, and the other that requires the width, the height, the DPIX, the DpiY, the PixelFormat and the BitmapPalette.

So I tried to use the second one adding ".FromResource("Data/flower2.png");", but it still not work: it shows me execution errors (one with Just My Code, when I disabled JMC, it shows me a weird error).

The screenshots of the two errors are there:

http://snap.ashampoo.com/ZcKXhOGq
http://snap.ashampoo.com/o920B8xv

Could you help me? I'm a bit confused...

Oh, I've also read an article on an Italian magazine that shows how to make something like Instagram using WriteableBitmapEx (in Windows Phone 8). I'm still not able to work following it because of the overloads.

Thanks a lot.

Jymmy097

New Post: Library doesn't work properly

$
0
0
Please use the BitmapFactory.New(1, 1) method. It handles all the rest. I will update the project main site. Sorry.

Check out the Source Code with the WPF samples. Those are great to use and adapt.
  • Rene

Updated Wiki: Home

$
0
0

Description

The WriteableBitmapEx library is a collection of extension methods for the WriteableBitmap. The WriteableBitmap class is available for Windows Phone, WPF, WinRT Windows Store XAML and Silverlight and allows the direct manipulation of a bitmap and could be used to generate fast procedural images by drawing directly to a bitmap. The WriteableBitmap API is very minimalistic and there's only the raw Pixels array for such operations. The WriteableBitmapEx library tries to compensate that with extensions methods that are easy to use like built in methods and offer GDI+ like functionality. The library extends the WriteableBitmap class with elementary and fast (2D drawing) functionality, conversion methods and functions to combine (blit) WriteableBitmaps.
The extension methods are grouped into different CS files with a partial class. It is possible to include just a few methods by using the specific source CS files directly or all extension methods through the built library assembly.

Available as NuGet package.

WriteableBitmapEx was also ported to Windows Embedded.

See the Issue Tracker for a list of features that will be added in the future.

wbx_announcement.png

Features

GDI+ like drawing functionality for the WriteableBitmap.
Support for Windows Phone Silverlight, desktop Silverlight, WPF and Windows WinRT XAML.
  • Base
    • Support for the Color structure (alpha premultiplication will be performed)
    • Also overloads for faster int32 as color (assumed to be already alpha premultiplied)
    • SetPixel method with various overloads
    • GetPixel method to get the pixel color at a specified x, y coordinate
    • Fast Clear methods
    • Fast Clone method to copy a WriteableBitmap
    • ForEach method to apply a given function to all pixels of the bitmap
  • Transformation
    • Crop method to extract a defined region
    • Resize method with support for bilinear interpolation and nearest neighbor
    • Rotate in 90° steps clockwise and any arbitrary angle
    • Flip vertical and horizontal
  • Shapes
    • Fast line drawing algorithms including an anti-aliased algorithm
    • Ellipse, polyline, quad, rectangle and triangle
    • Cubic Beziér, Cardinal spline and closed curves
  • Filled shapes
    • Fast ellipse and rectangle fill method
    • Polygon, triangle and quad
    • Beziér and Cardinal spline curves
  • Blitting
    • Different blend modes including alpha, additive, subtractive, multiply, mask and none
    • Optimized fast path for non blended blitting
  • Conversion
    • Convert a WriteableBitmap to a byte array
    • Create a WriteableBitmap from a byte array
    • Create a WriteableBitmap easily from the application resource or content
    • Create a WriteableBitmap from an any platform supported image stream
    • Write a WriteableBitmap as a TGA image to a stream
    • Separate extension method to save as a PNG image. Download here
  • Windows Phone specific methods
    • Save to media library and the camera roll

Live samples

Samples that come with the WriteableBitmapEx source code in action:
  • The Shapes sample includes various scenarios where different shapes are drawn. By default a little demo is shown called "Breathing Flower". Basically different sized circles rotating around a center ring are generated. The sample also contains a static page showing some of the possible shapes.
  • The Fill sample starts with a demo that animates the Cardinal spline's tension of the FillCurveClosed method, plus some random animated filled ellipses. The sample also contains a static page showing some of the possible filled shapes.
  • The Curve sample demonstrates the Beziér and Cardinal spline methods. The sample starts with a demo animation that uses the Cardinal spline DrawCurve method to draw an artificial plant that grows procedurally. The other part of the sample is interactive and allows to draw and manipulate Beziér and Cardinal splines with the mouse. See this blog post for further information.
  • The Blit sample combines WriteableBitmaps and shows a neat particle effect.
Video of the Windows Phone Interactive Curve Sample.

External resources:
Adam Kinney made a great sample that uses the WriteableBitmapEx library to dynamically apply a torn weathered effect to a photo.
Erik Klimczak from Calrity Consulting wrote a very good blog post about Advanced Animation: Animating 15,000 Visuals in Silverlight. He uses the WriteableBitmapEx to get the best performance.
Peter Bromberg wrote a great article called Silverlight 4 Martin Fractals with WriteableBitmapEx.

Performance!

The WriteableBitmapEx methods are much faster than the XAML Shape subclasses. For example, the WriteableBitmapEx line drawing approach is more than 20-30 times faster as the Silverlight Line element. So if a lot of shapes need to be drawn and anti-aliasing or other SIlverlight Shape properties are not needed, the WriteableBitmapEx methods are the right choice.

Easy to use!

// Initialize the WriteableBitmap with size 512x512 and set it as source of an Image control
WriteableBitmap writeableBmp = BitmapFactory.New(512, 512);
ImageControl.Source = writeableBmp;
writeableBmp.GetBitmapContext();

// Load an image from the calling Assembly's resources only by passing the relative path
writeableBmp = BitmapFactory.New(1, 1).FromResource("Data/flower2.png");

// Clear the WriteableBitmap with white color
writeableBmp.Clear(Colors.White);

// Set the pixel at P(10, 13) to black
writeableBmp.SetPixel(10, 13, Colors.Black);

// Get the color of the pixel at P(30, 43)
Color color = writeableBmp.GetPixel(30, 43);

// Green line from P1(1, 2) to P2(30, 40)
writeableBmp.DrawLine(1, 2, 30, 40, Colors.Green);

// Line from P1(1, 2) to P2(30, 40) using the fastest draw line method with the color as integerint[] pixels = writeableBmp.Pixels;
int w = writeableBmp.PixelWidth;
int h = writeableBmp.PixelHeight;
WriteableBitmapExtensions.DrawLine(pixels, w, h, 1, 2, 30, 40, myIntColor);

// Blue anti-aliased line from P1(10, 20) to P2(50, 70)
writeableBmp.DrawLineAa(10, 20, 50, 70, Colors.Blue);

// Black triangle with the points P1(10, 5), P2(20, 40) and P3(30, 10)
writeableBmp.DrawTriangle(10, 5, 20, 40, 30, 10, Colors.Black);

// Red rectangle from the point P1(2, 4) that is 10px wide and 6px high
writeableBmp.DrawRectangle(2, 4, 12, 10, Colors.Red);

// Filled blue ellipse with the center point P1(2, 2) that is 8px wide and 5px high
writeableBmp.FillEllipseCentered(2, 2, 8, 5, Colors.Blue);

// Closed green polyline with P1(10, 5), P2(20, 40), P3(30, 30) and P4(7, 8)int[] p = newint[] { 10, 5, 20, 40, 30, 30, 7, 8, 10, 5 };
writeableBmp.DrawPolyline(p, Colors.Green);

// Cubic Beziér curve from P1(5, 5) to P4(20, 7) with the control points P2(10, 15) and P3(15, 0)
writeableBmp.DrawBezier(5, 5, 10, 15, 15, 0, 20, 7,  Colors.Purple);

// Cardinal spline through the points P1(10, 5), P2(20, 40) and P3(30, 30) with a tension of 0.5int[] pts = newint[] { 10, 5, 20, 40, 30, 30};
writeableBmp.DrawCurve(pts, 0.5,  Colors.Yellow);

// A filled Cardinal spline through the points P1(10, 5), P2(20, 40) and P3(30, 30) with a tension of 0.5
writeableBmp.FillCurveClosed(pts, 0.5,  Colors.Green);

// Blit a bitmap using the additive blend mode at P1(10, 10)
writeableBmp.Blit(new Point(10, 10), bitmap, sourceRect, Colors.White, WriteableBitmapExtensions.BlendMode.Additive);

// Override all pixels with a function that changes the color based on the coordinate
writeableBmp.ForEach((x, y, color) => Color.FromArgb(color.A, (byte)(color.R / 2), (byte)(x * y), 100));

// Present the WriteableBitmap!
writeableBmp.Invalidate();

// Take snapshotvar clone = writeableBmp.Clone();

// Save to a TGA image stream (file for example)
writeableBmp.WriteTga(stream);

// Crops the WriteableBitmap to a region starting at P1(5, 8) and 10px wide and 10px highvar cropped = writeableBmp.Crop(5, 8, 10, 10);

// Rotates a copy of the WriteableBitmap 90 degress clockwise and returns the new copyvar rotated = writeableBmp.Rotate(90);

// Flips a copy of the WriteableBitmap around the horizontal axis and returns the new copyvar flipped = writeableBmp.Flip(FlipMode.Horizontal);

// Resizes the WriteableBitmap to 200px wide and 300px high using a bilinear interpolation methodvar resized = writeableBmp.Resize(200, 300, WriteableBitmapExtensions.Interpolation.Bilinear);

// Invalidate
writeableBmp.Dispose();

Additional Information

The WriteableBitmapEx library has its origin in several blog posts that also describe the implemenation and usage of some aspects in detail. The blog posts might be seen as the documentation.
WriteableBitmap Extension Methods introduced the SetPixel methods.
Drawing Lines - Silverlight WriteableBitmap Extensions II provided the DrawLine methods.
Drawing Shapes - Silverlight WriteableBitmap Extensions III brought the shape functionality (ellipse, polyline, quad, rectangle, triangle).
Convert, Encode And Decode Silverlight WriteableBitmap Data came with the byte array conversion methods and hows how to encode / decode a WriteableBitmap to JPEG.
Blitting and Blending with Silverlight’s WriteableBitmap provided the Blit functions.
WriteableBitmapEx - WriteableBitmap extensions now on CodePlex announced this project.
Quick and Dirty Output of WriteableBitmap as TGA Image provided the original TgaWrite function.
Rounder, Faster, Better - WriteableBitmapEx 0.9.0.0 announced version 0.9.0.0 and gives some further information about the curve sample.
Let it ring - WriteableBitmapEx for Windows Phone introtuced the WriteableBitmapEx version for the Windows Phone and a sample.
Filled To The Bursting Point - WriteableBitmapEx 0.9.5.0 announced version 0.9.5.0, has some information about the new Fill methods and comes with a nice sample.
One Bitmap to Rule Them All - WriteableBitmapEx for WinRT Metro Style announced version 1.0.0.0 and provides some background about the WinRT Metro Style version.

Support it

Donate

Credits

Bill Reiss wrote the Blit methods.
Nikola Mihaylov (Nokola) made some optimizations on the DrawLine and DrawRectangle methods, provided the original TgaWrite and the anti-aliased line drawing function.
Dr. Andrew Burnett-Thompson proposed the portability refactoring and WPF port.
Adam Kinney added some Blending modes to the Blit method.
Colin Eberhardt contributed the ForEach method.
Steve Hawley proposed an optimization of the Clear(Color) method.
Liam Bateman suggested the Color Keying BlendMode.
Mattias Fagerlund suggested the convolution method.
Wout de Zeeuw optimized the DrawLine method by 15%.
Your name here? We are always looking for valuable contributions.

René Schulte wrote all the rest and coordinates this project.

Ohloh statistics

New Post: Library doesn't work properly

$
0
0
Hi,

It still not work: it says that "Calls stack contains only external code. You have to disable 'only user code' and load symbols to view the origin of this path (or somehting similar: I have VS in Italian).
If I disable Just My code, it says that PresentatinFramework.pdb wasn't loaded.
C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.pdb: Unable to open or read the file PDB.
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PresentationFramework.pdb: Unable to open or read the file PDB.
C:\Windows\symbols\dll\PresentationFramework.pdb: Unable to open or read the file PDB.
C:\Windows\dll\PresentationFramework.pdb: Unable to open or read the file PDB.
C:\Windows\PresentationFramework.pdb: Unable to open or read the file PDB.

And then I stop the execution.
This is the code I use:

WriteableBitmap bmp = BitmapFactory.New(1, 1);
    public MainWindow()
    {
        InitializeComponent();
        bmp.FromResource("Image\\Tulips.jpg");
    }
and this is the XAML code:

<Window x:Class="Test45.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="900" Width="1366">
<Grid>
    <Button Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
    <Image HorizontalAlignment="Left" Height="768" Margin="90,10,0,0" VerticalAlignment="Top" Width="1024"/>

</Grid>
</Window>

What's going on?

Thanks a lot.

Jymmy097

New Post: Creating image snapshots from xaml control in WINRT

New Post: Creating image snapshots from xaml control in WINRT

$
0
0
Yep,

Great the Win XAml team is listening. They actually rewrote huge parts of the Xaml renderer.

I will make sure to add a .Render() extension function to the WinRT version of WBX to provide a similar API like WB has on other platforms.
  • Rene

New Post: Library doesn't work properly

$
0
0
I have the same problem ...
Is this a real bug? Or is the example outdated?

New Post: Library doesn't work properly

$
0
0
Jymmy097 wrote:
Hi,

It still not work: it says that "Calls stack contains only external code. You have to disable 'only user code' and load symbols to view the origin of this path (or somehting similar: I have VS in Italian).
If I disable Just My code, it says that PresentatinFramework.pdb wasn't loaded.
C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.pdb: Unable to open or read the file PDB.
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PresentationFramework.pdb: Unable to open or read the file PDB.
C:\Windows\symbols\dll\PresentationFramework.pdb: Unable to open or read the file PDB.
C:\Windows\dll\PresentationFramework.pdb: Unable to open or read the file PDB.
C:\Windows\PresentationFramework.pdb: Unable to open or read the file PDB.

And then I stop the execution.
This is the code I use:

WriteableBitmap bmp = BitmapFactory.New(1, 1);
    public MainWindow()
    {
        InitializeComponent();
        bmp.FromResource("Image\\Tulips.jpg");
    }
and this is the XAML code:

<Window x:Class="Test45.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="900" Width="1366">
<Grid>
    <Button Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
    <Image HorizontalAlignment="Left" Height="768" Margin="90,10,0,0" VerticalAlignment="Top" Width="1024"/>

</Grid>
</Window>

What's going on?

Thanks a lot.

Jymmy097

EDIT: I tried to add Tulips.jpg to one of the sample project and to load it using FromResource method.
This Window appeared:
http://snap.ashampoo.com/sGDY16Ai
It tells me to Find the origin of WriteableBitmapConvertExtensions.cs.
And If I do that, it tells me that Imust use BeginInit and EndInit or something similar because he couldn't find BitmapSource.

EDIT2:
I used this code:

bmp = BitmapFactory.New(1, 1).FromResource("Image\Tulips.jpg");

instead of this:

bmp.FromResource("Image\Tulips.jpg");

it tells me "Missing origin informations into debug informations for this module".
Found the solution in the samples. Here is how they do it:
WriteableBitmap LoadBitmap(string path)
      {
         var img = new BitmapImage();
         img.BeginInit();
         img.CreateOptions = BitmapCreateOptions.None;
         var s = Application.GetResourceStream(new Uri(path, UriKind.Relative)).Stream;
         img.StreamSource = s;
         img.EndInit();
         return BitmapFactory.ConvertToPbgra32Format(img);
      }

New Post: Is it possible to write text?

$
0
0
Does it exist simple way to render text on a WriteableBitmap?

New Post: Is it possible to write text?

$
0
0
Depends on the platform. For non-WinRT just render a TextBlock using the WB.Render() method. For WinRT since Win 8.1 use the new API for bitmap rendering.
  • rene

New Post: Is it possible to write text?

$
0
0
Sorry for ambiquous question. I mean WPF. Silverlight has Render method but WPF doesn't.

New Post: Is it possible to write text?

New Post: Is it possible to write text?

$
0
0
I'm trying to draw text in Silverlight. I blit an image and then I want to draw text on top of it
        TextBlock i = new TextBlock();
        i.Text = str;
        i.Foreground = new SolidColorBrush(color);
        wbmp.Render(i, new TranslateTransform() { X = x, Y = y });
But blitted background image is gone and text is drawn on completely black screen.
What do I do wrong? Thanks.
Viewing all 360 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>