How to get Image height and width issue in windows phone 7 - c#

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

Related

set resolution of image loaded on canvas wpf c#

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.

Saving writeablebitmap to a JPG in silverlight

I have a writeablebitmap object that I would like to save to JPG in silverlight , how can I do that?
I am also converting writeablebitmap object into image as
WriteableBitmap bitmap = new WriteableBitmap(Width,Height);
//Some operation of drawing on bitmap
and then
Image imageFHR = new Image();
imageFHR.Source = bitmap;
imageFHR.Height = Height;
imageFHR.Width = Width;
myCanvas.Children.Add(imageFHR);

Printing XPS document with image. Different image size when set different dpi

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

Convert BitmapImage to System.Windows.Media.Brush

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);

Assign dynamically generated image to ToggleButton.Content

I have to put dynamically generated image
var img = new Bitmap(..);
// draw something on img canvas ...
To the ToggleButton background. When I assign generated image to ToggleButton.Content property I see "System.Drawing.Bitmap" string, not the image itself. It looks like ToString() method is used for Content property. How can I show generated image instead?
If WPF does not have an appropriate converter it just calls the ToString() method, the Bitmap format is unsuitable, what you normally want to use is an Image with a source that is a BitmapImage, there are several ways to do conversions between the different formats.
Here is one method that does a conversion from Bitmap to BitmapImage:
public static BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
{
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage();
bImg.BeginInit();
bImg.StreamSource = new MemoryStream(ms.ToArray());
bImg.CreateOptions = BitmapCreateOptions.None;
bImg.CacheOption = BitmapCacheOption.Default;
bImg.EndInit();
ms.Close();
return bImg;
}
Note that ImageFormat.Png is slower than uncompressed formats but it retains the transparency if there is any.
Now you should be able to use this as the Source of an Image control and this Image control as the content of the button.
"Content" property is concerned with what you write on the surface of the ToggleButton. You need to initialize the "Background" property of the UI element. Here is one example:
PixelFormat pf = PixelFormats.Bgr32;
int width = 200;
int height = 200;
int rawStride = (width * pf.BitsPerPixel + 7) / 8;
byte[] rawImage = new byte[rawStride * height];
// Initialize the image with data.
Random value = new Random();
value.NextBytes(rawImage);
// Create a BitmapSource.
BitmapSource bitmap = BitmapSource.Create(width, height, 96, 96, pf, null, rawImage, rawStride);
ImageBrush imgBrush = new ImageBrush(bitmap);
myToggleButton.Background = imgBrush;
I created the image using the following article http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource(VS.85).aspx

Categories

Resources