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);
Related
So I have this code to crop images
private static Image Crop(Image img, Rectangle cropArea)
{
Bitmap bmpImage = (Bitmap)img.Clone();
var part = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
return part;
}
However, cropArea is occasionally out of bounds in the image, sometimes the crop extends outside the image and I want to fill these with transparent color, how do I achieve this?
Use Graphics object against the destination image and draw the original image at translated point.
private static Image Crop(Image img, Rectangle cropArea)
{
//Bitmap bmpImage = (Bitmap)img.Clone();
//var part = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
//return part;
Bitmap part = new Bitmap(cropArea.Width, cropArea.Height, img.PixelFormat);
using (var graphics = Graphics.FromImage(part))
{
graphics.DrawImage(img, -cropArea.X, -cropArea.Y);
}
return part;
}
After flipping a Bitmap I want to put it an ImageBox:
//set cam
Mat m = new Mat();
m = capture.QuerySmallFrame();
//Flip picture
Image img = m.ToImage<Bgr, byte>().Bitmap;
img.RotateFlip(RotateFlipType.Rotate180FlipY);
// show it
imageBox1.Image = img
When I run this, I get:
Can not implicitly convert type System.Drawing.Image to Emgu.CV.IImage.
The ImageBox.Image expects Emgu.CV.World.IImage, so I'd suggest doing the rotation using IImage's methods and avoiding to convert into System.Drawing.Bitmap altogether. The code below should do that:
//Flip picture
var img = m.ToImage<Bgr, byte>();
img = img.Rotate(180, new Bgr(Color.Red)).Flip(Emgu.CV.CvEnum.FlipType.Vertical);
// show it
imageBox1.Image = img;
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
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