Added ToStream and ToStreamAsJpeg for WinRT version
↧
Source code checked in, #107040
↧
New Post: What is the 'right' way to move an image blit'd onto another?
I have a large bitmap of a map that I am dropping objects onto. When the user drops the object, I pull the small bitmap (icon) associated with the object and blit it onto the map. Works fine.
Later if the user wants to move the icon, I detect that the user has clicked in the icons's Rect and as the mouse moves I want the icon to go with it until the user releases the mouse.
What is the 'right' way to make the icon move across the map? My instinct is to keep an undisturbed copy of the map in memory and then blit icon-sized patches from it over the old icon before blit'ing the icon again in the new location. I know that technique is straight out of the 80's... that is why I am asking if WriteableBitmapEx has a more sophisticated plan for this.
Dave
Later if the user wants to move the icon, I detect that the user has clicked in the icons's Rect and as the mouse moves I want the icon to go with it until the user releases the mouse.
What is the 'right' way to make the icon move across the map? My instinct is to keep an undisturbed copy of the map in memory and then blit icon-sized patches from it over the old icon before blit'ing the icon again in the new location. I know that technique is straight out of the 80's... that is why I am asking if WriteableBitmapEx has a more sophisticated plan for this.
Dave
↧
↧
New Post: Blit not producing anticipated results
Thanks, but according to your post at https://writeablebitmapex.codeplex.com/discussions/455204 you can blit images of different sizes. Is this not the case anymore? Also, can you elaborate on how I could use the Resize method to accomplish what I need?
↧
New Post: Blit not producing anticipated results
Sure you can blit different sizes, but the bitmaps won't be scaled. You need to either resize your target or destination bitmap to be the same size if you want them to overlay 1:1.
- Rene
↧
New Post: Blit not producing anticipated results
I managed to make this work by adding dummy "pushpins" to 2 diagonal corners of the InkManager to fake the manager into thinking it was the same size as my background. Now the user can mark up an image at will and the foreground (e.g. inkManager) will always be the same size as the background. When blitting the images, the foreground is then not stretched to the size of the background, preserving the scale and precision of the mark up.
Before blitting the images, I call this method:
Before blitting the images, I call this method:
private void addDummyCornerPins(WriteableBitmap backgroundBmp)
{
var windowHeightHalf = Window.Current.Bounds.Height / 2;
var windowWidthHalf = Window.Current.Bounds.Width / 2;
var imgHeightHalf = backgroundBmp.PixelHeight / 2;
var imgWidthHalf = backgroundBmp.PixelWidth / 2;
var ptListTopLeft = new List<Point>()
{
new Point(windowWidthHalf - imgWidthHalf, windowHeightHalf + imgHeightHalf)
};
var ptBottomRight = windowWidthHalf + imgWidthHalf + windowHeightHalf - imgHeightHalf;
var ptListBottomRight = new List<Point>()
{
new Point(windowWidthHalf + imgWidthHalf, windowHeightHalf - imgHeightHalf)
};
var builder = new InkStrokeBuilder();
var strokeTopLeft = builder.CreateStroke(ptListTopLeft);
var strokeBottomRight = builder.CreateStroke(ptListBottomRight);
CurrentInkManager.AddStroke(strokeTopLeft);
CurrentInkManager.AddStroke(strokeBottomRight);
}
↧
↧
New Post: Convolutions & Gaussian Blur
Trying to use convolute on one of the images in my project but the blur doesn't apply to the image at runtime.
StorageFile file = await StorageFile.GetFileFromPathAsync(Model.CurrentUser.BackgroundImagePath);
IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
WriteableBitmap wb = await new WriteableBitmap(1, 1).FromStream(fileStream);
var wb1 = WriteableBitmapExtensions.Convolute(wb, WriteableBitmapExtensions.KernelGaussianBlur3x3);
MainTile.Source = wb1;
Any ideas?↧
New Post: Alpha channel problem
I modified alpha channel directory. but i got strange result
caseA : Modified by WriteableBitmapEx drawing function -> OK (half side completely transparent)
caseB : Modified alpha channel directory (WriteableBitmapEx) -> NG (half side not completely transparent)
caseC : Modified alpha channel directory (Normal WriteableBitmap) ->OK (half side completely transparent)
what happned ?
WriteableBitmapEx vertion is 1.0.12.0
caseA : Modified by WriteableBitmapEx drawing function -> OK (half side completely transparent)
caseB : Modified alpha channel directory (WriteableBitmapEx) -> NG (half side not completely transparent)
caseC : Modified alpha channel directory (Normal WriteableBitmap) ->OK (half side completely transparent)
what happned ?
WriteableBitmapEx vertion is 1.0.12.0
public partial class MainWindow : Window {
WriteableBitmap chessBoard;
WriteableBitmap bitmap1;
WriteableBitmap bitmap2;
WriteableBitmap bitmap3;
public MainWindow() {
InitializeComponent();
chessBoard = MakeChessBordBitmap(100, 100, 5);
bitmap1 = BitmapFactory.New(100, 100);
bitmap2 = BitmapFactory.New(100, 100);
bitmap3 = new WriteableBitmap(100, 100, 72, 72, PixelFormats.Pbgra32, null);
using (bitmap1.GetBitmapContext()) {
bitmap1.Clear(Color.FromArgb(255, 255, 0, 0));
bitmap1.FillRectangle(0, 0, 100, 50, Color.FromArgb(0, 0, 0, 0));
}
using (bitmap2.GetBitmapContext()) {
bitmap2.Clear(Color.FromArgb(255, 0, 255, 0));
}
unsafe {
bitmap2.Lock();
for (int y = 0; y < 50; y++) {
for (int x = 0; x < 100; x++) {
byte* p = (byte*)bitmap2.BackBuffer + bitmap2.BackBufferStride * y + x * 4;
*(p + 3) = 0; // Alpha
}
}
bitmap2.AddDirtyRect(new Int32Rect(0, 0, 100, 100));
bitmap2.Unlock();
}
unsafe {
bitmap3.Lock();
for (int y = 0; y < 100; y++) {
for (int x = 0; x < 100; x++) {
byte* p = (byte*)bitmap3.BackBuffer + bitmap3.BackBufferStride * y + x * 4;
if (y < 50) {
*(p++) = 0; // B
*(p++) = 0; // G
*(p++) = 0; // R
*(p++) = 0; // Alpha
}
else {
*(p++) = 255; // B
*(p++) = 0; // G
*(p++) = 0; // R
*(p++) = 255; // Alpha
}
}
}
bitmap3.AddDirtyRect(new Int32Rect(0, 0, 100, 100));
bitmap3.Unlock();
}
caseA1.Source = chessBoard;
caseA2.Source = bitmap1;
caseB1.Source = chessBoard;
caseB2.Source = bitmap2;
caseC1.Source = chessBoard;
caseC2.Source = bitmap3;
}
// ChessBord
WriteableBitmap MakeChessBordBitmap(int w, int h, int n) {
WriteableBitmap bitmap = BitmapFactory.New(w, h);
int cx = w / n;
int cy = h / n;
int i, j;
int x2, y2;
using (bitmap.GetBitmapContext()) {
bitmap.Clear(Colors.White);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if ((i + j) % 2 == 0) {
y2 = Math.Min(cy * i + cy - 1, h - 1);
x2 = Math.Min(cx * j + cx - 1, w - 1);
bitmap.FillRectangle(j * cx, i * cy, x2, y2, Colors.Gray);
}
}
}
}
return bitmap;
}
}
<Window x:Class="AlphaBug.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="640">
<StackPanel Orientation="Horizontal">
<Viewbox Width="200" Height="200">
<Grid>
<Image x:Name="caseA1"/>
<Image x:Name="caseA2"/>
</Grid>
</Viewbox>
<Viewbox Width="200" Height="200" Margin="10,0,0,0">
<Grid>
<Image x:Name="caseB1"/>
<Image x:Name="caseB2"/>
</Grid>
</Viewbox>
<Viewbox Width="200" Height="200" Margin="10,0,0,0">
<Grid>
<Image x:Name="caseC1"/>
<Image x:Name="caseC2"/>
</Grid>
</Viewbox>
</StackPanel>
</Window>
↧
New Post: Modifying Brightness/Contrast dynamically - by using ToByteArray() /FromByteArray()
Hi there,
I am using WriteableBitmapEx as a base for a simple (wpf) image editor app, and it works wonderfully. However, recently I ran into a bit of a problem when trying to use ToByteArray() /FromByteArray() as a means to modify Brightness and Contrast of my images. Below is the code snippet with custom extension methods I wrote.
The problem that I have is that my Brightness and Contrast are just fine when I go into positive range. When the value is being changed towards lower end though ( negatives brightness ) the areas in my image that are supposed to turn black actually turn transparent! Is there a reason for this ? I also noticed that if I remove getting and setting up the alpha channel nothing changes at all. One of the issues of course is that the images I am trying to edit are greyscale, so technically I don't even need all three RGB values, but WriteableBitmapEx enforces Pbgra32 so I convert my images into it for editing. (Which is no big deal.) Btw, ToByteArray() /FromByteArray() works really really fast - I can actually drag the slider and see the change 'in real time', unlike when I use any other option. In comparison Get/SetPixel is virtually unusable here as it is extremely slow. But the pesky transparency issue I can do nothing about... Can somebody please help ?
I am using WriteableBitmapEx as a base for a simple (wpf) image editor app, and it works wonderfully. However, recently I ran into a bit of a problem when trying to use ToByteArray() /FromByteArray() as a means to modify Brightness and Contrast of my images. Below is the code snippet with custom extension methods I wrote.
The problem that I have is that my Brightness and Contrast are just fine when I go into positive range. When the value is being changed towards lower end though ( negatives brightness ) the areas in my image that are supposed to turn black actually turn transparent! Is there a reason for this ? I also noticed that if I remove getting and setting up the alpha channel nothing changes at all. One of the issues of course is that the images I am trying to edit are greyscale, so technically I don't even need all three RGB values, but WriteableBitmapEx enforces Pbgra32 so I convert my images into it for editing. (Which is no big deal.) Btw, ToByteArray() /FromByteArray() works really really fast - I can actually drag the slider and see the change 'in real time', unlike when I use any other option. In comparison Get/SetPixel is virtually unusable here as it is extremely slow. But the pesky transparency issue I can do nothing about... Can somebody please help ?
public unsafe static void SetBrightnessInContext(this WriteableBitmap bmp, int brightness)
{
if (brightness < -255) brightness = -255;
if (brightness > 255) brightness = 255;
byte[] pixeli = bmp.ToByteArray();
int l = pixeli.Length;
for (int i = 0; i < l; i++)
{
int pixel = pixeli[i];
int B = (int)(pixel & 0xFF); pixel >>= 8;
int G = (int)(pixel & 0xFF); pixel >>= 8;
int R = (int)(pixel & 0xFF); pixel >>= 8;
int A = (int)(pixel);
if (A == 0)
{
A = 1;
}
B += brightness;
R += brightness;
G += brightness;
if (R > 255) R = 255; if (G > 255) G = 255; if (B > 255) B = 255;
if (R < 0) R = 1; if (G < 0) G = 1; if (B < 0) B = 1;
byte r = (byte)(R);
byte g = (byte)(G);
byte b = (byte)(B);
byte a = (byte)(A);
pixeli[i] = (byte)((a << 24) | (r << 16) | (g << 8) | b); //(byte)(B | (G << 8) | (R << 16) | (A << 24));
}
bmp = bmp.FromByteArray(pixeli);
}
public unsafe static void SetContrastInContext(this WriteableBitmap bmp, double contrast)
{
if (contrast < -100) contrast = -100;
if (contrast > 100) contrast = 100;
contrast = (100.0 + contrast) / 100.0;
contrast *= contrast;
byte[] pixeli = bmp.ToByteArray();
int l = pixeli.Length;
for (int i = 0; i < l; i++)
{
int pixel = pixeli[i];
int B = (int)(pixel & 0xFF); pixel >>= 8;
int G = (int)(pixel & 0xFF); pixel >>= 8;
int R = (int)(pixel & 0xFF); pixel >>= 8;
int A = (int)(pixel);
R = (int)Math.Max(0, Math.Min(255, (((R - 128) * contrast) + 128)));
G = (int)Math.Max(0, Math.Min(255, (((G - 128) * contrast) + 128)));
B = (int)Math.Max(0, Math.Min(255, (((B - 128) * contrast) + 128)));
if (R > 255) R = 255; if (G > 255) G = 255; if (B > 255) B = 255;
if (R < 0) R = 1; if (G < 0) G = 1; if (B < 0) B = 1;
byte r = (byte)R;
byte g = (byte)G;
byte b = (byte)B;
byte a = (byte)A;
pixeli[i] = (byte)((a << 24) | (r << 16) | (g << 8) | b); //(byte)(B | (G << 8) | (R << 16) | (A << 24));
}
bmp = bmp.FromByteArray(pixeli);
}
↧
New Post: Draw multiple rotated ellipses
hi,
i got 5k ellipses and other shapes that i need to draw with an angle.
i see that there is "rotatefree" method but it works for the ALL bitmap.
any way to use it on a specific shape ?
regards,
benny.
i got 5k ellipses and other shapes that i need to draw with an angle.
i see that there is "rotatefree" method but it works for the ALL bitmap.
any way to use it on a specific shape ?
regards,
benny.
↧
↧
New Post: No alpha blending in the FillXXX methods
For VB.net, with modification:
Public Shared Sub FillRectangle2(ByRef bmp As WriteableBitmap, x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer, color As Color)
' Use refs for faster access (really important!) speeds up a lot!
Dim w As Integer = bmp.PixelWidth
Dim h As Integer = bmp.PixelHeight
Dim pixels As Integer() = bmp.Pixels
' Check boundaries
If x1 < 0 Then
x1 = 0
End If
If y1 < 0 Then
y1 = 0
End If
If x2 < 0 Then
x2 = 0
End If
If y2 < 0 Then
y2 = 0
End If
If x1 >= w Then
x1 = w - 1
End If
If y1 >= h Then
y1 = h - 1
End If
If x2 >= w Then
x2 = w - 1
End If
If y2 >= h Then
y2 = h - 1
End If
For y As Integer = y1 To y2
For i As Integer = y * w + x1 To y * w + x2
Dim oneOverAlpha As Byte = CByte(255 - color.A)
Dim c As Integer = pixels(i)
Dim r As Integer = ((((c >> 16) And 255) * oneOverAlpha) + (color.R * color.A)) >> 8
Dim g As Integer = ((((c >> 8) And 255) * oneOverAlpha) + (color.G * color.A)) >> 8
Dim b As Integer = ((((c >> 0) And 255) * oneOverAlpha) + (color.B * color.A)) >> 8
pixels(i) = 255 << 24 Or r << 16 Or g << 8 Or b
Next
Next
End Sub
↧
Source code checked in, #107895
Updated NuGet settings for WP 8.1
↧
New Post: High Resolution Image Load - OutOfMemory
i must read in my Photo Viewer high resolution image(20 MP - 41MP). The image are into IsolatedStorage
if i use this code
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
How to resolve?
if i use this code
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var stream = store.OpenFile(this.location, FileMode.Open))
{
var bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
bitmapImage.CreateOptions = BitmapCreateOptions.None;
var bmp = new WriteableBitmap((BitmapSource)bitmapImage);
bitmapImage.UriSource = (Uri)null;
return bmp;
}
}
receive OutOfMemoryHow to resolve?
↧
New Post: High Resolution Image Load - OutOfMemory
That's just too much to load into memory at once.
Checkout the Nokia Imaging SDK about how you can load such huge images.
Checkout the Nokia Imaging SDK about how you can load such huge images.
↧
↧
Created Unassigned: Drawing an XOR line on WriteableBitmapEx [20911]
I'm currently using WriteableBitmapEx in a project with pretty good success. I'm using it to drawing an array of simple charts very quickly and it seems to be working well. At this point, I'm trying to add some mouse interaction. I want to be able to drag a "selection rectangle" over the image in order to select certain charts. It seems that drawing a rectangle is not hard, but as I drag, I'd like to "erase" the previous rectange and then draw the new one. Back in the day, this would commonly be done by xor drawing the lines to make the rectangle appear, then xor drawing the same lines again would make the lines disappear and put back the original pixels that the lines were drawn over. It doesn't appear that the WriteableBitmapEx line drawing algorithms have an xor mode. So maybe there's a way to copy the bitmap (or portion of it that the rectangle occupies) to a buffer then refreshing it (to erase the previous rectangle) before drawing a new rectangle.
I'm looking for a good way to handle this.
Thanks for any advice.
I'm looking for a good way to handle this.
Thanks for any advice.
↧
New Post: WriteableBitmapEx and Windows RT
WriteableBitmapEx is available on WinRT Windows Store XAML,
does it mean that it is supported by corresponding on OS Windows RT 8.1(ARM processor) ?
does it mean that it is supported by corresponding on OS Windows RT 8.1(ARM processor) ?
↧
New Post: WriteableBitmapEx and Windows RT
Yes.
↧
New Post: RPC_E_WRONG_THREAD Thrown When I Try to Create New WriteableBitmap.
I'm having the same problem. How can I call this from a WRC
Thanks
Thanks
↧
↧
New Post: blit rt
Hi guys,
I would like to load some images stored in my project and after that compose it with blit function. Can you show me some example how to do it? I saw some examples in section "Source Code", but I didnt get it what is the format of the Uri.
Thnx
I would like to load some images stored in my project and after that compose it with blit function. Can you show me some example how to do it? I saw some examples in section "Source Code", but I didnt get it what is the format of the Uri.
Thnx
↧
New Post: blit rt
Please download the source code and see the dedicated bit samples there. They show exactly what you are looking for.
↧
New Post: WinRT Blit image quality
Hello,
I might not see the trees for the wood here. I have several smaller WriteableBitmaps and want to blit them into a larger one in a Windows Store 8.1 app.
When I copy pixel by pixel, I get the result on the right in the following image. When using Blit, I get the left one. Both sections are not the same but there is an obvious quality difference :-/. What am I doing wrong?
Any help appreciated.
Andreas
Source code:
Blit:
I might not see the trees for the wood here. I have several smaller WriteableBitmaps and want to blit them into a larger one in a Windows Store 8.1 app.
When I copy pixel by pixel, I get the result on the right in the following image. When using Blit, I get the left one. Both sections are not the same but there is an obvious quality difference :-/. What am I doing wrong?
Any help appreciated.
Andreas
Source code:
Blit:
dest.DrawInto(backgroundImage, xDestOffset, yDestOffset, 0, 0, backgroundImage.PixelWidth, backgroundImage.PixelHeight);
public static void DrawInto(this WriteableBitmap dest, WriteableBitmap backgroundImage, int xDestOffset, int yDestOffset, int xstart, int ystart, int xend, int yend)
{
Rect destRect = new Windows.Foundation.Rect(xDestOffset, yDestOffset, xDestOffset + xend-xstart, yDestOffset + yend-ystart);
Rect srcRect = new Windows.Foundation.Rect(xstart, ystart, xend-xstart, yend-ystart);
dest.Blit(destRect, backgroundImage, srcRect, WriteableBitmapExtensions.BlendMode.None);
}
Get/SetPixel: for (int x = 0; x < backgroundImage.PixelWidth; x++)
{
if (x +xDestOffset >= dest.PixelWidth) {
break;
}
for (int y = 0; y < backgroundImage.PixelHeight; y++)
{
if (y + yDestOffset >= dest.PixelHeight)
{
break;
}
var color = backgroundImage.GetPixel(x, y);
dest.SetPixel(x+xDestOffset, y+yDestOffset, color);
}
}
↧