I am working on canvas and loading an image on it. How do I set resolution of my image to 640X480 pixels? decodepixelheight and decodepixelwidth not working.
ImageBrush brush = new ImageBrush();
BitmapImage src = new BitmapImage(new Uri(("C:\\Users\\i2v\\Desktop\\GoogleMapTA.jpg"), UriKind.Relative));
src.DecodePixelHeight = 480;
src.DecodePixelWidth = 640;
brush.ImageSource = src;
// brush.Stretch = Stretch.None;
canvas.Background = brush;
canvas.Height = src.Height;
canvas.Width = src.Width;
BitmapImage implements the System.ComponentModel.ISupportInitialize interface. This means that its properties can only be set between calls of its BeginInit and EndInit methods:
var src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(#"C:\Users\i2v\Desktop\GoogleMapTA.jpg");
src.DecodePixelHeight = 480;
src.DecodePixelWidth = 640;
src.EndInit();
canvas.Background = new ImageBrush(src);
Note that you would usually not set DecodePixelWidth and DecodePixelHeight at the same time, since this might disrupt the native aspect ratio of the image. Set either one or the other.
Related
I want to set width to dynamically added button background image.
this is my code
Buttob btn=new Button();
ImageBrush brush1 = new ImageBrush();
brush1.ImageSource = new BitmapImage(new Uri("ms-px:///Assets/emptyseat.jpg"));
btn.Background = brush1;
how to set width of the above image dynamically.
You can scale the image by creating a ScaleTransform object and applying it to the imageBrush, and setting the Stretch property on your brush to whatever it is you desire.
For example:
Button btn = new Button();
ImageBrush brush1 = new ImageBrush();
brush1.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/emptyseat.jpg"));
ScaleTransform scaleTransform = new ScaleTransform();
scaleTransform.ScaleX = 0.5;
brush1.Transform = scaleTransform;
brush1.Stretch = Stretch.Uniform;
btn.Background = brush1;
It's not entirely clear what you are trying to achieve but the above will resize the image for you.
On my windows store app, im using a bings maps, and im trying to add a pushpin with an image
to do so , im doing this piece of code
Bing.Maps.Location center = new Bing.Maps.Location();
center.Latitude = 40.130066068147585;
center.Longitude = -8.338623046875;
Map.Center = center;
Map.ZoomLevel = 12D;
var pushpinLayer = new MapLayer();
pushpinLayer.Name = "PushPinLayer";
Map.Children.Add(pushpinLayer);
var location = new Location(40.130066068147585D, -8.338623046875D);
Image pinImage = new Image();
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.UriSource = new Uri("ms-appx:///Assets/POI_Red_Ipad#2x.png", UriKind.RelativeOrAbsolute);
pinImage.Width = 20;
pinImage.Height = 30;
pinImage.Source = bitmapImage;
pushpinLayer.Children.Add(pinImage);
it adds the pushpin image but it appears on the top left corner of the map, i dont know how to set its position to use the localtion variable :\
Ok so you just have things a little out of order. The first part is correct:
Bing.Maps.Location center = new Bing.Maps.Location();
center.Latitude = 40.130066068147585;
center.Longitude = -8.338623046875;
Map.Center = center;
Map.ZoomLevel = 12D;
Next, instead of creating a maplayer you would instead create your pushpin image:
Image pinImage = new Image();
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.UriSource = new Uri("ms-appx:///Assets/POI_Red_Ipad#2x.png", UriKind.RelativeOrAbsolute);
pinImage.Width = 20;
pinImage.Height = 30;
pinImage.Source = bitmapImage;
Then you can create your location:
var location = new Location(40.130066068147585D, -8.338623046875D);
Here is where it is different. You do not need to create an instance of the MapLayer class, instead assign the element (image in your case) and location - then add it to to the map.
MapLayer.SetPosition(pinImage, location);
Map.Children.Add(pinImage);
I printing XPS document with images, that contains QR-codes.
Image creating sample here:
Image image = CreatePresentation(qrCode);
image.Height = 200;
image.Width = 200;
image.HorizontalAlignment = HorizontalAlignment.Center;
image.VerticalAlignment = VerticalAlignment.Center;
image.Stretch = Stretch.None;
where
public static Image CreatePresentation(System.Drawing.Image source)
{
Stream stream = new MemoryStream();
source.Save(stream, ImageFormat.Png);
Image image = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.StreamSource = stream;
src.EndInit();
image.Source = src;
return image;
}
When i have monitor dpi=96 then image size on printed page (on paper after printing) more larger than when i have montor dpi=120.
How i can print images with the same size on different dpi?
When i replaced
image.Stretch = Stretch.None;
on
image.Stretch = Stretch.Fill;
image began print correct
I am trying to get the height and width of an image in windows phone...but there are few syntax errors
how should i do this?
int hight = image1.ActualHeight;
int width = image1.ActualWidth;
BitmapImage img = new BitmapImage(image1.Image);
BitmapImage newImg = new BitmapImage(hight,width);
To create an image,
<Image x:Name="image1" Source="myPicture.png" />
And then you can access it in your code behind
double height = image1.ActualHeight;
double width = image1.ActualWidth;
And there is no constructor for BitmapImage class which takes the arguments that you are passing. You can create a new BitmapImage in either of the following ways
BitmapImage bmp = new BitmapImage(new Uri("myPicture.jpg", UriKind.RelativeOrAbsolute));
or
BitmapImage bmp = new BitmapImage();
bmp.UriSource = new Uri("myPicture.jpg", UriKind.RelativeOrAbsolute);
or
BitmapImage bitmapImage = image1.Source as BitmapImage;
Hope this clears your doubt
How would I convert a BitmapImage to a System.Windows.Media.Brush?
I have a BitmapImage imaginatively called bitmap, and I have a Canvas (also imaginatively titled) canvas. How would I set the value of canvas to the value of bitmap?
I've tried canvas.Background = bitmap;, but that didn't work: image.Source = bitmap; works for images, but not Canvases: and ImageSourceConverter imgs = new ImageSourceConverter();
canvas.SetValue(Image.SourceProperty, imgs.ConvertFromString(bitmap.ToString()));
didn't work either.All of these worked with images, however.Maybe something with bitmap.ToString() would work?
Create an ImageBrush and use that as the background:
ImageBrush ib = new ImageBrush();
ib.ImageSource = bitmap;
canvas.Background = ib;
Simply:
canvas.Background = new ImageBrush(bitmap);