I have both a CameraCaptureTask and a PhotoChooserTask in my app where you can either take or load a photo. The end goal of all of this is to finally send it to an Azure Web API.
The photo that I get back from both of these tasks is very large (about 4mb) and about 3000x2000 pixels in dimensions.
Is there a way to get a much smaller version of the picture? This is how I handle the chooser;
public void PhotoChooserTaskCompleted(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
var bmp = new BitmapImage();
bmp.SetSource(e.ChosenPhoto);
var writeableBitmap = new WriteableBitmap(bmp);
...
Is there a way to load a smaller 'thumbnail' version or do I have to manipulate the image and resize it?
Thanks for any pointers.
WriteableBitmap bmpWritable = new WriteableBitmap(bmp);
MemoryStream ms = new MemoryStream();
Extensions.SaveJpeg(bmpWritable, ms, newWidth, newHeight, 0, 100);
I would suggest you resize it proportionally.
Hope this helps.
And also, You need not to have two separate tasks for taking image from Camera and from gallery. For PhotoChooserTask itself, you can set ShowCamera attribute to true. This will display a camera button when in ApplicationBar when you open gallery
Related
I'm creating a desktop application in JavaFX which enables the user to search for people in different categories. There is a screen which shows each category as a tile with an image (Aspect ratio 1:1). When you click on a tile it opens another page and the image from the tile should now be displayed as the background image (Aspect ratio 16:9). The images are selected by an admin user, so it must be cropped because it could be too large, have a wrong aspect ratio and so on.
I wonder how to set up an easy way to enable the admin user to select the picture he wants, without having to crop the image twice (once as 1:1 and once as 16:9). I thought about crop only to 1:1 and then for displaying as 16:9 just zooming the picture, but this leads to bad quality if the resolution isn't high enough.
For cropping I'm referencing to this post from Roland:
How to make a Javafx Image Crop App
For background images you can simply specify that the image should cover the Region.
ImageView allows you to specify a viewport allowing you to specify the region of the Image that should be displayed. If chosen accordingly this does the croping for you.
The following code uses the same ratio for both for simplicity's sake:
#Override
public void start(Stage primaryStage) {
final Image image = new Image(URL);
// size to use for both nodes
double targetHeight = 400;
double targetWidth = targetHeight * 16 / 9;
ImageView imageView = new ImageView(image);
imageView.setFitWidth(targetWidth);
imageView.setFitHeight(targetHeight);
// calculate viewport
imageView.setViewport((image.getHeight() / targetHeight < image.getWidth() / targetWidth)
? new Rectangle2D(0, 0, image.getHeight() / targetHeight * targetWidth, image.getHeight())
: new Rectangle2D(0, 0, image.getWidth(), image.getWidth() / targetWidth * targetHeight));
Region backgroundRegion = new Region();
backgroundRegion.setPrefSize(targetWidth, targetHeight);
backgroundRegion.setBackground(new Background(
new BackgroundImage(
image,
BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT,
BackgroundPosition.CENTER,
new BackgroundSize(0, 0, false, false, false, true) // cover
)));
HBox root = new HBox(imageView, backgroundRegion);
root.setFillHeight(false);
root.setPadding(new Insets(20));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
how can crop image using c# windows phone
crop rect from image
how can crop face from image
the code :
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.ChosenPhoto);
imgShow.ImageSource = bmp;
var wb = new WriteableBitmap(bmp);
var sdkImg = ImageConverter.SystemToSdk(wb);
IFaceDetector detector = FaceDetectorFactory.Create(FaceDetectionType.Haar);
var gray = new ImageGray(sdkImg);
FaceRect[] rc = detector.Detect(gray);
MessageBox.Show(rc[0].ToString());
faceRect.Margin = new Thickness(rc[1].Rect.Left / 2, rc[1].Rect.Top / 2, 0, 0);
faceRect.Width = rc[1].Rect.Width;
faceRect.Height = rc[1].Rect.Height;
faceRect.Visibility = System.Windows.Visibility.Visible;
You could simply do it using the WriteableBitmap class which is inherited from BitmapSource. There are many samples out there, which you could follow.
References:
WindowsPhone: Image crop with rectangle
Crop Image Area with Different Shapes
Crop an Image using the WriteableBitmap
In my application there is a possibility to create screenshot of current view. I implement it with WriteableBitmap class. For example:
var frame = Application.Current.RootVisual as PhoneApplicationFrame; WriteableBitmap bitmap = new WriteableBitmap(frame, null);
BitmapImage result = new BitmapImage();
using (var stream = new MemoryStream())
{
bitmap.SaveJpeg(stream, (int)frame.ActualWidth, (int)frame.ActualHeight, 0, 100);
result.SetSource(stream);
}
The problem is that if on page, that now is displaying, there is an application bar, it doesn't display on saved image. Any ideas what should I do with this issue?
Application bar is not a part of your app, so, you can't do that. The only way is to ask user to make a screenshot by pressing Windows+Camera buttons.
I want to merge two images,one image is of 300x300 and other is 100x100, First i have created a canvas and then i created two images which i have added to the both the images to canvas and the canvas is added to the content panel, then i created a writeablebitmap and render the canvas and created a method savejpeg which saves the image to isolated stoarage,but isolated storage is not showing the whole image it save a black screen.
First i created a canvas through code set its height width and background color then i created two images programmatically which i have added to the canvas and then canvas is added to the contentpanel
my code is:
public void CreateImage()
{
Canvas canvas = new Canvas();
canvas.Height = 400;
canvas.Width = 400;
canvas.Background = new SolidColorBrush(Colors.Red);
Image img1 = new Image();
img1.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("Image/Desert.jpg");
img1.Height = 300;
img1.Width = 300;
img1.Margin = new Thickness(0, 10, 0, 0);
Image img2 = new Image();
img2.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("Image/Jellyfish.jpg");
img2.Height = 50;
img2.Width = 50;
img2.Margin=new Thickness(0,10,300,0);
canvas.Children.Add(img1);
canvas.Children.Add(img2);
ContentPanel.Children.Add(canvas);
WriteableBitmap wb = new WriteableBitmap(400, 400);
wb.Render(canvas, new MatrixTransform());
MemoryStream ms = new MemoryStream();
wb.SaveJpeg(ms,400,400,0,100);
using (var isoFileStream = new IsolatedStorageFileStream("myPicture.jpg", FileMode.OpenOrCreate, IsolatedStorageFile.GetUserStoreForApplication()))
{
wb.SaveJpeg(isoFileStream, 400, 400, 0, 100);
}
}
When i save the image then i am getting a black screen in isolated storage.
How to save both images on canvas?
Like Stephan said, I think you are not getting the image to your source. Any way I created a sample application for you. In that you can find two partitions, you can add image to that by double tapping on the container. After that try save and check your saved image. I tested the app and every thing is working for me. Still you face any kind of issues please leave a comment.
https://www.dropbox.com/s/1vjbbou96w0r15r/SaveImageApp.zip
Please check weather you are getting image or not to image source. If you are getting the image; try this method to take snapshot from control and save that to Iso store.
http://stackoverflow.com/questions/13837148/how-can-i-take-a-screenshot-full/13990649#13990649
I'm using the HubTile control from the silverlight toolkit and somehow the title is displayed upside down in my own app, but also in the preview. See Screenshots below.
I'm generating the tile from code behind and adding it to a WrapPanel control:
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(stream);
var hubtile = new HubTile()
{
Source = bitmap,
Height = AppConfig.TileHeight,
Width = AppConfig.TileWidth,
Background = App.Current.Resources["PhoneAccentBrush"] as SolidColorBrush,
Margin = new Thickness(5),
IsFrozen = true,
Title = "Lorem Ipsum"
};
wpTiles.Children.Add(hubtile);
The upside down part is the back of the tile.
It is visible because you haven't set a background to the front of the tile.
Be sure to set the background of the front and back of the tile (to something that isn't transparent) to avoid this scenario.