ImageBrush for Grid programmatically - c#

So I try to apply an image but cannot see any changes...
What do I am missing? Thanks!!
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(#"pack://application:,,,/Images/bg1.jpg", UriKind.RelativeOrAbsolute);
bi.EndInit();
ImageBrush ib = new ImageBrush();
ib.TileMode = TileMode.Tile;
ib.ImageSource = bi;
ib.Stretch = Stretch.None;
RootGrid.Background = ib;

Try this instead:
var ib = new ImageBrush {
ImageSource =
new BitmapImage(
new Uri(#"Images\bg1.jpg", UriKind.Relative)
)
};
RootGrid.Background = ib;
Also, this is obvious, but make sure the image is actually at the right path and set to be Content in the Project.

Related

DockPanel ImageOption.Image is not showing the image

I'll make it short,
My problem is: The image that i want to set in my DockPanel as icon is not showing.
Here is my Code
DockPanel dock = new DockPanel();
dock.ImageOption.Image = <Bitmap image here>; //
dock.Controls.Add(UserControl1);
UserControl1.MyDockPanel = dock;
Any expert opionion here?
Thanks
You can add image to DockPanel in the cs like this:
DockPanel dock = new DockPanel();
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri("...\\ico_48.ico", UriKind.RelativeOrAbsolute);
myBitmapImage.DecodePixelWidth = 2048;
myBitmapImage.EndInit();
Image image = new Image();
image.Source = myBitmapImage;
dock.Children.Add(image);
Content = dock;

Autosize Button with an image

How to make a buttons auto-size with an image instead of fixing a default button size? Thanks really appreciated.
All my buttons are fixed size, I need to have some buttons buttons to be big, some small depending on the image size.
heres my codes:
Button tba = new Button();
tba.FontSize = 19;
tba.Height = 300
tba.MinWidth = 100;
//tba.Height = Double.NaN;
//tba.Width = Double.NaN;
ImageBrush brush = new ImageBrush();
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(#"files.png" + lstQuestion[i].ImageURL.Substring(1), UriKind.Absolute);
bitmap.EndInit();
brush.ImageSource = bitmap;
tba.Background = brush;
wrapPanel1.Children.Add(tba);
To size the button to the image: host the image in an Image and set Image.Strech to None and remove the sizes from your Button:
Button tba = new Button();
Image myImage = new Image();
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(#"files.png" + lstQuestion[i].ImageURL.Substring(1), UriKind.Absolute);
bitmap.EndInit();
myImage.Source = myBitmapImage;
myImage.Stretch = Stretch.None;
tba.Content = myImage;
If you want more than just an image in your Button add the image to panel that will let the image fill the space, e.g. a Grid, then set Button.Content to that panel.
Incidentally in XAML you can just go:
<Button>
<Image Strech="None" Source=".\Water Lilies.jpg"/>
</Button>

Why doesn't my image display in the grid?

I can't find the reason why the image won't display in the grid I created
Image img = new Image();
img.Source = new BitmapImage(new Uri(#"C:\logo.bmp", UriKind.Relative));
img.Width = 50;
img.Height = 50;
img.Margin = new Thickness(5, 5, 5, 5);
grid1.Children.Add(img);
Your image URI is an absolute path.
You should write
img.Source = new BitmapImage(new Uri(#"C:\logo.bmp", UriKind.Absolute));
or better
img.Source = new BitmapImage(new Uri(#"C:\logo.bmp"));

WPF Add a png image from resources with C# at runtime

I'm trying to add an image in a stack panel at runtime. My image is in the resources of the application. Here's the code that I have for the moment:
Image image = new Image();
ImageSourceConverter isc = new ImageSourceConverter();
image.Source = isc.ConvertFrom(Properties.Resources.entity16_10) as ImageSource;
image.Height = 16;
image.Width = 16;
panel.Children.Add(image);
I've got a null pointer on the line where I'm trying to use the converter, I don't know if it's the good way of doing this.
Here's how i do it :
object imguri = new Uri("/MyAssembly;Component/MyImageFolder/MyImage.png", UriKind.Relative);
BitmapImage ni = new BitmapImage(imguri);
Image img = new Image();
img.Source = ni;
return img;

How can I convert WriteableBitmap to BitmapImage?

BitmapImage bitmapImage = new BitmapImage(new Uri("arka_projects_as_logo.png", UriKind.Relative));
Image uiElement = new Image() { Source = bitmapImage };
ScaleTransform t = new ScaleTransform() { ScaleX = 0.2, ScaleY = 0.2 };
WriteableBitmap writeableBitmap = new WriteableBitmap(uiElement,t);
I want to insert the result of this conversions (writeableBitmap) into System.Windows.Controls.Image. When I do this:
Image arkaImage = new Image() { Source = writeableBitmap };
arkaImage isn't shown at all. What can be done to make it work?
WriteableBitmap wb = ..
using (MemoryStream ms = new MemoryStream())
{
wb.SaveJpeg(ms, (int)image1.Width, (int)image1.Height, 0, 100);
BitmapImage bmp = new BitmapImage();
bmp.SetSource(ms);
}
Why don't you just apply the ScaleTransform to the UIElement as well?

Categories