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

Edited Feature: Possibility to FillPolygon with holes [17402]

$
0
0
It would be very useful to be able to draw polygons that have holes inside (Map or GIS applications).
Comments: ** Comment from web user: teichgraf **

We added DrawPolygonEvenOdd which supports that.


Closed Unassigned: Clear doesnt seem to work for large images [20649]

$
0
0
If I have a wpf window with an Image control (of a fixed smallish size) with the source bound to a writeablebitmap and I fill the writeablebitmap with a colour, then when the writeablebitmap is smallish (width less that 100000) then the window renders as expected but if the writeablebitmap is large (width of 100000) then I just see weird artefacts in the Image control.
Comments: This is rather a limit of the underlying platform. Huge bitmaps are not supported on WP for example.

Closed Unassigned: PNG rendered incorrectly [21480]

$
0
0
Hi,

I am having trouble rendering PNGs that use Palette as "Color Type". Here is some simple code to reproduce the issue:

```
private async System.Threading.Tasks.Task Fetch()

{

HttpClient httpClient = new HttpClient();

Uri uri = new Uri("http://static.splashnology.com/articles/How-to-Optimize-PNG-and-JPEG-without-Quality-Loss/PNG-Palette.png");

HttpResponseMessage response = await httpClient.GetAsync(uri);

if (response.StatusCode == HttpStatusCode.Ok)

{

try

{

var content = await response.Content.ReadAsBufferAsync();

WriteableBitmap image = await BitmapFactory.New(1, 1).FromStream(content.AsStream());

Rect destination = new Rect(0, 0, image.PixelWidth, image.PixelHeight);

Rect source = new Rect(0, 0, image.PixelWidth, image.PixelHeight);

WriteableBitmap canvas = new WriteableBitmap(image.PixelWidth, image.PixelHeight);

canvas.Blit(destination, image, source);

RadarImage.Source = canvas;

}

catch (Exception e)

{

System.Diagnostics.Debug.WriteLine(e.Message);

System.Diagnostics.Debug.WriteLine(e.StackTrace);

}

}

}
```

If I run that code using Windows Phone 8.1, the image appears using wrong colors. If I do the same test using a PNG that is using RGB as "Color Type", then everything is works fine.
Comments: Not an issue of this lib. This an underlying limitation of WP.

Source code checked in, #113259

$
0
0
* Added usage of CohenSutherlandLineClip to DrawLineAa and DrawLine

Edited Feature: Different behavior of DrawLine* methods with points outside bitmap [20684]

$
0
0
Hi!

We came across an issue with DrawPolyline. When the array contains points that are outside the bitmap the lines is not correct. It seems that there is a problem with he slope of the line. This also leaves holes between the lines of the polyline. I have prepared a demo that demonstrates this problem. While I was doing the demo I noticed, that the different DrawLine methods return different results when provided with the same points.

* __DrawLineDDA__ and __DrawLineBresenham__ work always correctly.
* __DrawLine__ fluctuate depending on the slope of the line
* __DrawLineAa__ is clipped incorrectly and simply doesn't work in scenario with points outside the bitmap.

The problem can be reproduced both in SL and WPF with the latest version of the library (1.0.9.0).
Comments: ** Comment from web user: teichgraf **

Just fixed this in chageset 113259 by using CohenSutherlandLineClip for DrawLineAa and DrawLine

Created Unassigned: how to import the library in wpf and vb.net ? [21772]

$
0
0
so i used Nugget to add the library to my project and it's added in references, but i can't import it as there is no namespace that contains it
any help ?

Created Unassigned: FromStream broken for Wpf [21778]

$
0
0
I am desperately trying to load a WriteableBitmap with the FromStream extension method.

The code below throws a System.InvalidOperationException at the FromStream() call with the following information:

__ BitmapImage has not been initialized. Call the BeginInit method, set the appropriate properties, and then call the EndInit method.__

I verified that the image exists and it's build action is set to Resource. The image is valid (I loaded it into a BitmapImage) and the stream has the correct length.

var sri = Application.GetResourceStream(new Uri("dummy.jpg", UriKind.RelativeOrAbsolute));
if (sri != null)
{
using (var stream = sri.Stream)
{
// creates the bitmap, part of the writeablebitmapextensions on WPF
var wb = BitmapFactory.New(1, 1);
wb.FromStream(stream); // <--- Exception
}
}

The error seems to indicate the extension method uses a BitmapImage internally in an incorrect way, BeginInit and EndInit are probably missing

Closed Unassigned: how to import the library in wpf and vb.net ? [21772]

$
0
0
so i used Nugget to add the library to my project and it's added in references, but i can't import it as there is no namespace that contains it
any help ?
Comments: It's all in the WriteableBitmap's original
namespace System.Windows.Media.Imaging

Source code checked in, #113386

$
0
0
* Fixed bug #21778 where FromStream for WPF needs to be called inside Init scope

Edited Issue: FromStream broken for Wpf [21778]

$
0
0
see also [this stackoverflow question](http://stackoverflow.com/questions/29079938/weird-writeablebitmap-fromstream-issue) for the bugfix.

I am desperately trying to load a WriteableBitmap with the FromStream extension method.

The code below throws a System.InvalidOperationException at the FromStream() call with the following information:

__ BitmapImage has not been initialized. Call the BeginInit method, set the appropriate properties, and then call the EndInit method.__

I verified that the image exists and it's build action is set to Resource. The image is valid (I loaded it into a BitmapImage) and the stream has the correct length.

var sri = Application.GetResourceStream(new Uri("dummy.jpg", UriKind.RelativeOrAbsolute));
if (sri != null)
{
using (var stream = sri.Stream)
{
// creates the bitmap, part of the writeablebitmapextensions on WPF
var wb = BitmapFactory.New(1, 1);
wb.FromStream(stream); // <--- Exception
}
}

The error indicates the extension method uses a BitmapImage internally in an incorrect way, BeginInit and EndInit are missing
Comments: ** Comment from web user: teichgraf **

Thanks for raising this. This was just fixed in the latest source code and will be part of the next release. If you need it now, just get latest source code and rebuild the lib locally or take the #if WPF part of the FromStream method into your code.

New Post: AccessViolationException when accessing pixel color (GetPixel())

$
0
0
Ive been trying to follow this suggestion but havent been able to so far. How should I change it to make this work?

The crazy thing here is that this work for a larger image (full hd) but not for a 512x424 image.

This is the code im using:
 private void SumPictures( WriteableBitmap currPicture )
        {     
            for (int col = 0; col < currPicture.PixelWidth; col++)
            {
                for (int row = 0; row < currPicture.PixelHeight; row++)
                {
                    Color currPixel = currPicture.GetPixel(col, row);  //<-- Error happens here. The console output prints the PixelWidth and PixelHeight correctly
                    Console.WriteLine("Col: " + col + " row: " + row + " currPicture.PixelWidth: " + currPicture.PixelWidth + " currPicture.PixelHeight: " + currPicture.PixelHeight);

                    _sumedAllImages[col][row].R += currPixel.R;
                    _sumedAllImages[col][row].G += currPixel.G;
                    _sumedAllImages[col][row].B += currPixel.B;
                    _sumedAllImages[col][row].A += currPixel.A; 
                }
            }
        }

        private void LoadAndAverageImages(string filePrefix)
        {
            string[] allPictures = Directory.GetFiles(selectFolder(), filePrefix + "*.png", SearchOption.AllDirectories);            

            //for (int i = 0; i < allPictures.Length; i++)
            Parallel.For(0, allPictures.Length, i =>
            {
                _filenameList.Add(allPictures[i]);
                WriteableBitmap currPicture = new WriteableBitmap((BitmapSource)(new BitmapImage(new Uri(allPictures[i]))));
                SumPictures(currPicture);
                if (i == 0)
                    _samplePicture = currPicture;
                Console.WriteLine("currPicture: " + i);
            });           

            for (int col = 0; col < _samplePicture.PixelWidth; col++)
            {
                for (int row = 0; row < height; row++)
                {
                    Color currPixelSum = new Color();
                    currPixelSum.R = (byte)_sumedAllImages[col][row].R;
                    currPixelSum.G = (byte)_sumedAllImages[col][row].G;
                    currPixelSum.B = (byte)_sumedAllImages[col][row].B;
                    currPixelSum.A = (byte)_sumedAllImages[col][row].A;

                    _samplePicture.SetPixel(col, row, currPixelSum);
                }
            }

            Console.WriteLine("Loaded " + allPictures.Length + " pictures!");
        }
Thank you!

New Post: AccessViolationException when accessing pixel color (GetPixel())

$
0
0
Same issues. You want to access the pixels but the loading is asynchronously, so the pixel data was not loaded.
            WriteableBitmap currPicture = new WriteableBitmap((BitmapSource)(new __BitmapImage(new Uri(allPictures[i]))));__
Sometimes it might have been loaded but that's good luck.

You should rather use the WBX method FromStream(stream)
Also, you can't create a WB on a background thread so running the creation of WBs needs to happen before your Parallel loop on the UI thread.

Something like this (not tested nor compiled);
var wbs = new List<WriteableBitmap>();
foreach(var fileName in Directory.GetFiles(selectFolder(), filePrefix + "*.png", SearchOption.AllDirectories))
{
    using(var stream = File.OpenRead(fileName))
    {
          var wb = BitmapContext.New(1, 1).FromStream(stream);
          wbs.Add(wb);
    }
}

Parallel.For(0, wbs.Count, i =>
            {
                var currPicture = wbs[i];
                SumPictures(currPicture);
                if (i == 0)
...

New Post: AccessViolationException when accessing pixel color (GetPixel())

$
0
0
So the error I had when opening a new Image is now gone, but Im still seeing the same error when getting a single pixel from the loaded image.

The code is basicly the same you posted, but I had to make my own FromStream method, which goes as follows:
 public static WriteableBitmap MyFromStream(WriteableBitmap bmp, Stream stream)
        {
            var bmpi = new BitmapImage();
                bmpi.BeginInit();
                bmpi.StreamSource = stream;
                bmpi.EndInit();
            bmp = new WriteableBitmap(bmpi);
            return bmp;
        }
And its accessed like this:
var wbs = new List<WriteableBitmap>();
            for ( int i = 0; i < allPictures.Length; i++ )
            {
                using( var stream = File.OpenRead( allPictures[i]) )
                {
                    //var wb = BitmapFactory.New(1, 1).FromStream(stream);
                    var wb = MyFromStream(BitmapFactory.New(1, 1), stream);
                    wbs.Add(wb);
                }
            }
So the error is happening here now. What is the right way of accessing a single pixel? Just to make it crystal clear, I need to access the whole image, pixel by pixel.
 //for (int i = 0; i < allPictures.Length; i++)
            Parallel.For(0, wbs.Count, i =>
            {
                var currPicture = wbs[i];
                SumPictures(currPicture);
                if (i == 0)
                    _samplePicture = currPicture;
                Console.WriteLine("currPicture: " + i);
            });

private void SumPictures( WriteableBitmap currPicture )
        {     
            for (int col = 0; col < currPicture.PixelWidth; col++)
            {
                for (int row = 0; row < currPicture.PixelHeight; row++)
                {
                    Color currPixel = currPicture.GetPixel(col, row);  //<-- Error happens here.

                    _sumedAllImages[col][row].R += currPixel.R;
                    _sumedAllImages[col][row].G += currPixel.G;
                    _sumedAllImages[col][row].B += currPixel.B;
                    _sumedAllImages[col][row].A += currPixel.A; 
                }
            }
        }
And for last, but not least, when calling BitmapFactory.New(1,1), the description says that New receives the Width and Height of the newly created WriteableBitmap, so how come Im passing it as 1?

Thanks for the support!

Created Unassigned: Bug in Blit function for WPF [21788]

$
0
0
Hello,
there is bug in Blit function, when using BlendMode.Alpha.

instead of code

if (blendMode == BlendMode.Alpha)
{
var isa = 255 - sa;
#if NETFX_CORE
// Special case for WinRT since it does not use pARGB (pre-multiplied alpha)
destPixel = ((da & 0xff) << 24) |
((((sr * sa + isa * dr) >> 8) & 0xff) << 16) |
((((sg * sa + isa * dg) >> 8) & 0xff) << 8) |
(((sb * sa + isa * db) >> 8) & 0xff);
#else
destPixel =
((((sa << 8) + isa * da) >> 8) << 24) |
(((((sr << 8) + isa * dr) >> 8) & 0xff) << 16) |
(((((sg << 8) + isa * dg) >> 8) & 0xff) << 8) |
((((sb << 8) + isa * db) >> 8) & 0xff);
#endif
}

should be

if (BlendMode == BlendMode.Alpha)
{
var isa = 255 - sa;
#if NETFX_CORE
// Special case for WinRT since it does not use pARGB (pre-multiplied alpha)
destPixel = ((da & 0xff) << 24) |
((((sr * sa + isa * dr) >> 8) & 0xff) << 16) |
((((sg * sa + isa * dg) >> 8) & 0xff) << 8) |
(((sb * sa + isa * db) >> 8) & 0xff);
#else
destPixel = ((da & 0xff) << 24) |
(((((sr * sa) + isa * dr) >> 8) & 0xff) << 16) |
(((((sg * sa) + isa * dg) >> 8) & 0xff) << 8) |
((((sb * sa) + isa * db) >> 8) & 0xff);
#endif
}

(instead of (((sr << 8) + isa * dr) >> 8) should be (((sr * sa + isa * dr) >> 8), difference in A component is not subject of this issue
Tested with some icons, fixed version renders nice blended icons

Source code checked in, #113508

$
0
0
* Fixed bug in Blit Alpha code for WPF when source format is not pre-multiplied alpha

Edited Issue: Bug in Blit function for WPF [21788]

$
0
0
Hello,
there is bug in Blit function, when using BlendMode.Alpha.

instead of code

```
if (blendMode == BlendMode.Alpha)
{
var isa = 255 - sa;
#if NETFX_CORE
// Special case for WinRT since it does not use pARGB (pre-multiplied alpha)
destPixel = ((da & 0xff) << 24) |
((((sr * sa + isa * dr) >> 8) & 0xff) << 16) |
((((sg * sa + isa * dg) >> 8) & 0xff) << 8) |
(((sb * sa + isa * db) >> 8) & 0xff);
#else
destPixel =
((((sa << 8) + isa * da) >> 8) << 24) |
(((((sr << 8) + isa * dr) >> 8) & 0xff) << 16) |
(((((sg << 8) + isa * dg) >> 8) & 0xff) << 8) |
((((sb << 8) + isa * db) >> 8) & 0xff);
#endif
```

should be

```
if (BlendMode == BlendMode.Alpha)
{
var isa = 255 - sa;
#if NETFX_CORE
// Special case for WinRT since it does not use pARGB (pre-multiplied alpha)
destPixel = ((da & 0xff) << 24) |
((((sr * sa + isa * dr) >> 8) & 0xff) << 16) |
((((sg * sa + isa * dg) >> 8) & 0xff) << 8) |
(((sb * sa + isa * db) >> 8) & 0xff);
#else
destPixel = ((da & 0xff) << 24) |
(((((sr * sa) + isa * dr) >> 8) & 0xff) << 16) |
(((((sg * sa) + isa * dg) >> 8) & 0xff) << 8) |
((((sb * sa) + isa * db) >> 8) & 0xff);
#endif
}
```

(instead of (((sr << 8) + isa * dr) >> 8) should be (((sr * sa + isa * dr) >> 8), difference in A component is not subject of this issue
Tested with some icons, fixed version renders nice blended icons
Comments: ** Comment from web user: teichgraf **

I assume you were using it on WPF?
Keep in mind that the WB uses pre-multiplied so the RGB components are already multiplied with their alpha values on most platforms, so performing sr * sa actually means (sr*sa) * sa. I added an additional check for the WPF build if the source.Format is pRGBA. This should work for all cases now. Please get latest source code from the repository and try it.
Thanks for raising this issue.

Commented Issue: Bug in Blit function for WPF [21788]

$
0
0
Hello,
there is bug in Blit function, when using BlendMode.Alpha.

instead of code

```
if (blendMode == BlendMode.Alpha)
{
var isa = 255 - sa;
#if NETFX_CORE
// Special case for WinRT since it does not use pARGB (pre-multiplied alpha)
destPixel = ((da & 0xff) << 24) |
((((sr * sa + isa * dr) >> 8) & 0xff) << 16) |
((((sg * sa + isa * dg) >> 8) & 0xff) << 8) |
(((sb * sa + isa * db) >> 8) & 0xff);
#else
destPixel =
((((sa << 8) + isa * da) >> 8) << 24) |
(((((sr << 8) + isa * dr) >> 8) & 0xff) << 16) |
(((((sg << 8) + isa * dg) >> 8) & 0xff) << 8) |
((((sb << 8) + isa * db) >> 8) & 0xff);
#endif
```

should be

```
if (BlendMode == BlendMode.Alpha)
{
var isa = 255 - sa;
#if NETFX_CORE
// Special case for WinRT since it does not use pARGB (pre-multiplied alpha)
destPixel = ((da & 0xff) << 24) |
((((sr * sa + isa * dr) >> 8) & 0xff) << 16) |
((((sg * sa + isa * dg) >> 8) & 0xff) << 8) |
(((sb * sa + isa * db) >> 8) & 0xff);
#else
destPixel = ((da & 0xff) << 24) |
(((((sr * sa) + isa * dr) >> 8) & 0xff) << 16) |
(((((sg * sa) + isa * dg) >> 8) & 0xff) << 8) |
((((sb * sa) + isa * db) >> 8) & 0xff);
#endif
}
```

(instead of (((sr << 8) + isa * dr) >> 8) should be (((sr * sa + isa * dr) >> 8), difference in A component is not subject of this issue
Tested with some icons, fixed version renders nice blended icons
Comments: ** Comment from web user: dbshell **

Hello,
thank you for explanation.
I have used version from official downloads, with some fixes and extension. When upgrading to latest source code version, it works perfectly.
Only when upgrading, FillRectangle function draws rectangles one pixel smaller than previous rectangle... I fixed it in my calling code.
Jan

Commented Issue: Bug in Blit function for WPF [21788]

$
0
0
Hello,
there is bug in Blit function, when using BlendMode.Alpha.

instead of code

```
if (blendMode == BlendMode.Alpha)
{
var isa = 255 - sa;
#if NETFX_CORE
// Special case for WinRT since it does not use pARGB (pre-multiplied alpha)
destPixel = ((da & 0xff) << 24) |
((((sr * sa + isa * dr) >> 8) & 0xff) << 16) |
((((sg * sa + isa * dg) >> 8) & 0xff) << 8) |
(((sb * sa + isa * db) >> 8) & 0xff);
#else
destPixel =
((((sa << 8) + isa * da) >> 8) << 24) |
(((((sr << 8) + isa * dr) >> 8) & 0xff) << 16) |
(((((sg << 8) + isa * dg) >> 8) & 0xff) << 8) |
((((sb << 8) + isa * db) >> 8) & 0xff);
#endif
```

should be

```
if (BlendMode == BlendMode.Alpha)
{
var isa = 255 - sa;
#if NETFX_CORE
// Special case for WinRT since it does not use pARGB (pre-multiplied alpha)
destPixel = ((da & 0xff) << 24) |
((((sr * sa + isa * dr) >> 8) & 0xff) << 16) |
((((sg * sa + isa * dg) >> 8) & 0xff) << 8) |
(((sb * sa + isa * db) >> 8) & 0xff);
#else
destPixel = ((da & 0xff) << 24) |
(((((sr * sa) + isa * dr) >> 8) & 0xff) << 16) |
(((((sg * sa) + isa * dg) >> 8) & 0xff) << 8) |
((((sb * sa) + isa * db) >> 8) & 0xff);
#endif
}
```

(instead of (((sr << 8) + isa * dr) >> 8) should be (((sr * sa + isa * dr) >> 8), difference in A component is not subject of this issue
Tested with some icons, fixed version renders nice blended icons
Comments: ** Comment from web user: dbshell **

Sorry for typing error is mean:
FillRectangle function draws rectangles one pixel smaller than previous __version__

Edited Feature: FillRectangle incorrectly includes right/bottom [21276]

$
0
0
If you call FillRectangle and pass a rectangle with width 1 and height 1, it instead fills an area 2 pixels wide and 2 pixels high.

It appears that WriteableBitmapEx's FillRectangle is including x2 and y2 in its rectangle. This is inconsistent with both System.Drawing and WPF, as well as every other graphics library I've ever used (GDI, GDI+, Delphi's TCanvas, ImageMagick, PyGame, etc.) FillRect calls typically do not fill the right-most column or the bottom-most row. That's because rectangle coordinates typically represent the grid lines between pixels, and that's because the math is far, far easier that way. (Raymond Chen has a good explanation at http://blogs.msdn.com/b/oldnewthing/archive/2004/02/18/75652.aspx.)

Repro (using the latest NuGet release, 1.0.12.0):

var bitmap = BitmapFactory.New(2, 2);
// Should fill an area (1 - 0) pixels wide and (1 - 0) pixels high
bitmap.FillRectangle(0, 0, 1, 1, Colors.Red);
MyImage.Source = bitmap;

Expected: a 2x2 bitmap with one red pixel in the top left.

Actual: a 2x2 bitmap completely filled with red.

Released: WriteableBitmapEx 1.5.0 (Mar 31, 2015)

$
0
0
!This is the final version 1.5.0 of WriteableBitmapEx with WPF, Silverlight, WinRT Windows and Windows Phone XAML and Silverlight support
  • Supports a variety of versions including Windows 10 UAP, Windows 8, 8.1, Windows Phone WinRT and Silverlight 7, 8, 8.1
  • Added lots of contributions including DrawLine with variable thickness, penning, improved anti-aliasing and Wu's anti-aliasing algorithm.
  • Added usage of CohenSutherlandLineClip for DrawLineAa and DrawLine
  • Added support for alpha blended filled shapes and adapted the FillSample for WPF
  • Added FillPolygonsEvenOdd() which uses the even-odd algorithm to fill complex polygons with more than one closed outline like for the letter O
  • Added AdjustBrightness(), AdjustContrast() and AdjustGamma() methods
  • Added Gray() method which returns the gray scaled version the bitmap
  • Fixed bug in Blit Alpha code for WPF when source format is not pre-multiplied alpha
  • Fixed bug #21778 where FromStream for WPF needs to be called inside Init scope
  • Fixed issue with IndexOutOfRangeEx in DrawLine method
  • Fixed Invalidate for Silverlight BitmapContext.Dispose
  • Fixed regression issue with alpha blending for Blit for non-WinRT
  • Fixed many more reported issues https://writeablebitmapex.codeplex.com/workitem/list/basic

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

Blog posts with more details:
http://kodierer.blogspot.com/2015/03/staying-alive-writeablebitmapex-15.html
Viewing all 360 articles
Browse latest View live


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