transparent png using wpf in VS2008 - wpf

I want make a UI that is semi transparent in WPF VS2008, so I made my form transparent and I want to show a semi transparent png (Which includes "holes") on top of it.
How do I show the semi transparent png?
Semi transparent, meaning it has holes you can see through.
Also how can I get this done in C#, without using WPF.
Thanks.

You should just have to use the Image control and WPF should take care of the rest:
<Image Source="myimage.png" />
Or in pure C#:
BitmapImage sourceImage = new BitmapImage();
sourceImage.BeginInit();
sourceImage.UriSource = new Uri("myimage.png", UriKind.RelativeOrAbsolute);
sourceImage.EndInit();
Image myImage = new Image();
myImage.Source = sourceImage;

Related

when to use WriteableBitmap and BitmapImage in silverlight

I am trying to display image using BitmapImage for some time and it worked.I have changed the image and it stopped working.
For Bitmapimage I was using this code:
`ms.Seek(0, SeekOrigin.Begin); // ms is memory stream
BitmapImage b = new BitmapImage();
b.SetSource(ms);
image.ImageSource = b;`
I have ran into piece of code where it was checking if the length of the bytes[] ==14400
if(bytes.length == 14400)
{
var bmp = new WriteableBitmap(width, height);
Buffer.BlockCopy(buffer, 0, bmp.Pixels, 0, buffer.Length);
}
I want to know when to use WriteableBitmap and BitmapImage .
From iProgrammer:
Bitmaps are generally implemented as immutable objects. What this means is that once you create a bitmap you can't make any changes to it. You can manipulate bitmaps by creating new versions, which then immediately become immutable....
The WriteableBitmap, as its name suggests, isn't immutable and you can get at its individual pixels and manipulate them as much as you want. This is the ideal way to work when you need dynamic bitmaps.
iProgrammer - WriteableBitmap
From MSDN:
"Use the WriteableBitmap class to update and render a bitmap on a per-frame basis..." MSDN - WriteableBitmap Class
The Examples section of the MSDN article also shows how to update a WritableBitmap image when responding to mouse events. The code in the example erases pixels of the image by setting the pixel's ARGB values to 0 when the mouse's right button is down. The code also shows how to update individual pixels in the image where the mouse's left button is down. Essentially the code shows a rudimentary pixel image editor.
The point, however, is that you can't change image data when using regular bitmap - you have to use WritableBitmap instead. You can, however, render both if you wish.

Dark vertical line on a panorama control background image edge in Windows Phone

I set a panorama backgroundBrush the following way:
In DataContext:
var bitmapImage = new BitmapImage(new Uri("../Images/panorama.background.png", UriKind.RelativeOrAbsolute));
var backBrush = new ImageBrush { ImageSource = bitmapImage };
PanoramaBackgroundBrush = backBrush;
In View:
<controls:Panorama Background="{Binding PanoramaBackgroundBrush}"
The panorama.background.png is a white image with an app logo on top.
Whenever I scroll over the edge of Panorama background (from last panorama item to the first one), a vertical thin dark line appears at a time of motion. When the transition stops, the line disappears.
You can see the problem on a video of a simulator I have uploaded to youtube.
It is almost as if the rendering engine of WP SL is not catching up to redraw the transition of the image's edge. But the same background is in the Office Hub, and it doesn't have a problem I am experiencing.
Please advise on how to solve the vertical line problem.
I can reproduce this and sometimes even got bigger gaps while testing.
I think the office hub suffers from the same problem but is faking it away by using a white background. And you can do so, too:
<Grid x:Name="LayoutRoot" Background="White">
This gives the grid behind the Panorama a white background. If your image is white at the edge (like in the YouTube video) then the glitch won't be visible anymore.

save writableimage to file in silverlight

How want to save the image of a canvas to a file.
var img = new WriteableBitmap(canvas1, null);
Image i = new Image();
i.Source = img;
var bitmap = new Bitmap(i);
I tried to use bitmap.Save( for saving the image but Bitmap is not supported by silverlight.
How would you save WriteableBitmap to a file?
The WriteableBitmap has a Pixels collection which can be used to access the rendered image. However you really need to get it stored in a known format (preferable PNG).
The imagetools codeplex project can do that for you.
See this blog for a simple example of using it for your purposes.

With C# / Winforms / GDI+, how do i do getpixel or getimage of a control?

I want to 'grab' the image of a control on my winforms dialog. I can access the 'graphics' context for the control using:
MyControl.CreateGraphics()
But how do i copy a rectangle from that graphics context to an image, or a bitmap, or call getpixel on it?
Thanks a lot.
I think MyControl.DrawToBitmap is the way to go:
Bitmap bmp = new Bitmap(MyControl.Width, MyControl.Height);
MyControl.DrawToBitmap(bmp, MyControl.ClientRectangle);
If you need to get the pixels then use Bitmap.GetPixel or Bitmap.LockBits

Image Flipping in WPF

How do you get an image to be reflected in wpf without using the xaml? I have a rectangle with a BitmapImageBrush as its fill. I want to be able to regularly flip this image (X-axis) back and forth at will in C#. I've read about using the ScaleTransform property but this only seems to work in the xaml and that's not what I want.
This is how the image is created:
ImageBrush l = new ImageBrush(new BitmapImage(new Uri(uriPath, UriKind.Relative)));
_animation.Add(l);
_visual = new Rectangle();
_visual.Fill = _animation[0];
_visual.Width = width;
_visual.Height = height;
_visual.Visibility = Visibility.Visible;
_window.GameCanvas.Children.Add(_visual);
_animation is a list of ImageBrushes.
This is really simple, yet I can't seem to figure out how to do it. Please help.
You can still add a scale transform programmatically, rather than using xaml.
For instance, in this article.
To do the flip, set your scale transform to be negative in the direction you want to flip (ie, if horizontal, set x to -1, or -desiredScale if you also want to resize).

Resources