Resize image and change background Color - windows-phone-7

I finish from resizing image but how can I change Background Color (Change black Color)?
Like this image:
http://i.stack.imgur.com/2Y3xG.jpg
BitmapImage img = new BitmapImage();
img.SetSource(e.ChosenPhoto);
Image image2 = new Image()
{
Width=640,Height=640,Visibility=Visibility.Collapsed,Source=img
};
WriteableBitmap wb1 = new WriteableBitmap(image2,st);
wb1.SaveJpeg(ms1, 640, 640, 0, 100);

you must place your Image control inside a container control (like a grid) and change the background color of that container.

Related

JavaFX How to crop image once for multiple aspect ratios

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

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

how to merge two images in windows phone and save it to isolated storage

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

HTML5 canvas, scale image after drawing it

I'm trying to scale an image that has already been draw into canvas.
This is the code:
var canvas = document.getElementById('splash-container');
var context = canvas.getContext('2d');
var imageObj = new Image();
imageObj.onload = function() {
// draw image at its original size
context.drawImage(imageObj, 0, 0);
};
imageObj.src = 'images/mine.jpeg';
// Now let's scale the image.
// something like...
imageObj.scale(0.3, 0.3);
How should I do?
You're thinking about it wrong. Once you've drawn the image onto the canvas it has no relationship to the imageObj object. Nothing you do to imageObj will affect what's already drawn. If you want to scale the image, do in the drawImage function:
drawImage(imgObj, 0, 0, imgObj.width * 0.3, imgObj.height * 0.3)
If you want to animate the scaling or are looking to achieve some other effect which requires you to draw the image at full size initially you'll have to first clear it before drawing the scaled down image.
What robertc says is correct, but if you really wanted to scale an image on a canvas after drawing it for some reason, you could just scale the whole canvas using the CSS width/height properties and that would scale the image without having to redraw it.

How to make Flying Saucer generate an image with transparent background?

I'm using flyingsaucer to render HTML to an image, using Java2DRenderer:
Map<Key,Object> renderingHints = new HashMap<Key,Object>();
renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
renderingHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
renderingHints.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
hints = new RenderingHints(renderingHints);
Java2DRenderer renderer = new Java2DRenderer(dom, width, height);
renderer.setRenderingHints(renderingHints);
renderer.getSharedContext().setDPI(DPI);
renderer.setBufferedImageType(BufferedImage.TYPE_INT_ARGB);
img = renderer.getImage();
Then I add the rendered image to a PDF document, scaling it to fit the entire page.
com.lowagie.text.Document pdf = new com.lowagie.text.Document(isLandscape ? PageSize.A4.rotate() : PageSize.A4);
pdf.setMargins(MARGIN, MARGIN, MARGIN, MARGIN);
Rectangle ps = pdf.getPageSize();
PdfWriter.getInstance(pdf, outputStream);
pdf.open();
com.lowagie.text.Image pdfImage = com.lowagie.text.Image.getInstance(img, null);
pdfImage.scaleAbsolute(
ps.getWidth() - pdf.leftMargin() - pdf.rightMargin(),
ps.getHeight() - pdf.topMargin() - pdf.bottomMargin()
);
pdf.add(pdfImage);
pdf.close();
The problem is that the image rendered by FS has as background color solid white... so the printer fills the entire page with a very light gray (I believe that scaling transforms the solid white into a light grey).
How can I force flyingsaucer generate a transparent image so I can avoid this problem?
For the Java2DRenderer there is a way by subclassing the Java2DRender
final java.awt.Color TRANSPARENT = new Color(255, 255, 255, 0);
final int imageType = BufferedImage.TYPE_INT_ARGB;
final Java2DRenderer java2dRenderer = new Java2DRenderer(doc, width, height) {
#Override
protected BufferedImage createBufferedImage(final int width, final int height) {
final BufferedImage image = org.xhtmlrenderer.util.ImageUtil.createCompatibleBufferedImage(width, height, imageType);
org.xhtmlrenderer.util.ImageUtil.clearImage(image, TRANSPARENT);
return image;
}
};
java2dRenderer.setBufferedImageType(imageType);

Resources