I would like to display an image composed of two images.
I want image rectangle.png to show with image sticker.png on top of it with its left-hand corner at pixel 10, 10.
Here is as far as I got, but how do I combine the images?
Image image = new Image();
image.Source = new BitmapImage(new Uri(#"c:\test\rectangle.png"));
image.Stretch = Stretch.None;
image.HorizontalAlignment = HorizontalAlignment.Left;
Image imageSticker = new Image();
imageSticker.Source = new BitmapImage(new Uri(#"c:\test\sticker.png"));
image.OverlayImage(imageSticker, 10, 10); //how to do this?
TheContent.Content = image;
You need a Panel to add both Image controls to. A Grid or Canvas will allow this, but I would go with the Grid as it will constrain the Image controls (thereby stretching or shrinking them as appropriate).
Image image = new Image();
image.Source = new BitmapImage(new Uri(#"c:\test\rectangle.png"));
image.Stretch = Stretch.None;
image.HorizontalAlignment = HorizontalAlignment.Left;
Image imageSticker = new Image();
imageSticker.Source = new BitmapImage(new Uri(#"c:\test\sticker.png"));
imageStiker.Margin = new Thickness(10, 10, 0, 0);
Grid grid = new Grid();
grid.Children.Add(image);
grid.Children.Add(imageSticker);
TheContent.Content = grid;
You can just put one Image control over the top of the other Image control in your View. Place both into a Grid or Canvas, and just overlay one of the images on top of the other. This also lets you using opacity to do blending, and works very well.
If you need to get them into the same image, there are a couple of options....
You could make a WritableBitmap out of the first image, then manually "paint" the other image pixels on top of the first one. This then can act as an image source for your image in the display.
Alternatively, you could do the overlay I mentioned above, and render this into a RenderTargetBitmap. This could then be used as your image source.
Related
The customized controls have two images, a background and a foreground image. I use the following code to display the controls. This function is called twice, once for background image displaying and then for the foreground image displaying
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(imgpath, UriKind.Absolute);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
ImageBrush ib = new ImageBrush(src);
ib.Viewbox = new Rect(UVRectangle.X / src.PixelWidth, UVRectangle.Y / src.PixelHeight, UVRectangle.Width / src.PixelWidth, UVRectangle.Height / src.PixelHeight);
Image image = new Image();
image.Source = ib.ImageSource;
gr.Children.Add(image); //gr is of type Grid
The buttons are displayed perfectly but I realized that their alignment is incorrect. When I place one button right below another button, the one below appears to be shifted to the right.
This problem of alignment is rectified by changing
Image image = new Image();
image.Source = ib.ImageSource;
gr.Children.Add(image);
to
gr.Background = ib;
But, this makes the foreground image completely cover the background image like I had asked here
What should I do to prevent this alignment problem? Also, what is causing this problem in the first place?
Did you try putting the buttons inside stackpanel? http://msdn.microsoft.com/en-us/library/system.windows.controls.stackpanel.aspx
I want to show an image in a Grid
Only the image shows when I use this XAML:
<Image Source="/Assets/O.png"
Grid.Column="6"
Grid.Row="5"/>
In C# it shows the picture with a border outside:
Image img = new Image();
BitmapImage bi = new BitmapImage();
bi.UriSource = new Uri("/Assets/O.png", UriKind.Relative);
img.Stretch = Stretch.Fill;
img.Source = bi;
Grid.SetColumn(img, 6);
Grid.SetRow(img, 7);
gridGameBoard.Children.Add(img);
Image img1 = new Image();
img1.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Assets/X.png", UriKind.Relative));
Grid.SetColumn(img1, 4);
Grid.SetRow(img1, 4);
gridGameBoard.Children.Add(img1);
I tried googling to find any differences between BitmapImage and other Image types but was unsuccessful.
The problem is the Stretch property of your Image. In XAML, you do not define it, so a default value of Uniform is used. In C# code you have img.Stretch = Stretch.Fill;
My guess is that the border is in the image, but in the C# code with Fill the image is scaled down, the XAML variant with Uniform scales up the image and centers it so the border is not visible.
After looking for the answer in both Microsoft documentation and forums, I am at a loss. I am loading a png image as a background for an inkCanvas (WPF) which works fine, however, it always resizes the image to fit in the canvas, despite the image size..
Here was my last attempt with no success:
BitmapImage ii = new BitmapImage(new Uri(path));
Image img = new Image();
img.Stretch = Stretch.None;
img.Source = ii;
InkCanvas1.Background = new ImageBrush(ii);
Here is what it looks like using Stretch.None and Stretch.Fill
Here is what I am trying to achieve:
Can this be done?
The problem is that you're trying to set properties on an Image object that you're not using while ignoring the same settings on the ImageBrush that you are using. The Image in this case is just being thrown away and the ImageBrush just happens to be using the same source image. Set the Stretch property on the ImageBrush instead:
BitmapImage ii = new BitmapImage(new Uri(path));
ImageBrush imageBrush = new ImageBrush(ii);
imageBrush.Stretch = Stretch.Uniform;
InkCanvas1.Background = imageBrush;
I have several paragraphs of text and couple of pictures between these paragraphs.
Now, I want to generate a picture using these materials, merging them vertically. But all the blocks of the text and pictures can not have bigger width than that of the generating picture, which means I have to zoom out the origin pictures, and fill each paragraph of text into a rectangle to fit the width.
Here is the tough thing:
To figure out the size of the rectangle to contain the text, I need use Graphics.MeasureString() method, which needs an instance of Graphics used to generate my picture(now, I'm using a blank template picture). But I do not know the exact size of this Graphics until I figure out all the sizes of rectangles and pictures.
Is there any method to get an instance of Graphics without source image?
Or is there any other method to do this work?
hope this could help dude .
http://chiragrdarji.wordpress.com/2008/05/09/generate-image-from-text-using-c-or-convert-text-in-to-image-using-c/
https://web.archive.org/web/20131231000000/http://tech.pro/tutorial/654/csharp-snippet-tutorial-how-to-draw-text-on-an-image
http://www.codeproject.com/Questions/388845/HOW-TO-MAKE-HIGH-QAULITY-IMAGE-WITH-TEXT-IN-Csharp
thank you
For people how are intrested in a WPF solution (as asked):
public static BitmapSource CreateImage(string text, double width, double heigth)
{
// create WPF control
var size = new Size(width, heigth);
var stackPanel = new StackPanel();
var header = new TextBlock();
header.Text = "Header";
header.FontWeight = FontWeights.Bold;
var content = new TextBlock();
content.TextWrapping = TextWrapping.Wrap;
content.Text = text;
stackPanel.Children.Add(header);
stackPanel.Children.Add(content);
// process layouting
stackPanel.Measure(size);
stackPanel.Arrange(new Rect(size));
// Render control to an image
RenderTargetBitmap rtb = new RenderTargetBitmap((int)stackPanel.ActualWidth, (int)stackPanel.ActualHeight, 96, 96, PixelFormats.Pbgra32);
rtb.Render(stackPanel);
return rtb;
}
I'm new to WPF. I was programming for Windows Forms Applications, and there was Picture Box with Image property. But in WPF there is Image instead of Picture Box. How to get it's value? I mean the image data.
you can do that by using Image.Source property.
You will find a lot of samples on internet about it. For example code below sets image source, you can get the image similarly
Image myImage3 = new Image();
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri("smiley_stackpanel.PNG", UriKind.Relative);
bi3.EndInit();
myImage3.Stretch = Stretch.Fill;
myImage3.Source = bi3;
//get the source
BitmapImage imgSource = myImage3.Source;