Scaling encodedImage - codenameone

How can we scale encodedImage with respect to width and the height just scale with proportion to width? We can scaleWidth of image but there is no scaledEncodedWidth. Is there a way to scale width of encoded image and height scale with respect to the width?
code:
homePlaceholder = (EncodedImage) r.getImage("placeholder.png");
homePlaceholder = homePlaceholder.scaledEncoded(screenWidth / 3, screenWidth / 3);
//the following doesnt work as well
homePlaceholder = homePlaceholder.scaledEncoded(screenWidth / 3, -1);

I hope replacing EncodedImage with Image will solve your problem as shown in following code
Image homePlaceholder =(Image) r.getImage("placeholder.png");
homePlaceholder = homePlaceholder.scaledWidth(screenWidth / 3);
EncodedImage encodedHomePlaceholder = EncodedImage.createFromImage(homePlaceholder , true)

Related

Image magnify Glass on windows form

i am trying to use glass magnifier on PictureEdit(devexpress) control in windows form
we have set PictureEdit.SizeMode =Squeeze . its important. in our application i am already using Squeeze size mode.
i am getting problem of calculation of mouse location , for draw rect image onto enalged panel.
here is code of PartialMag_Paint event of enalaged panel. in which we show zoomed imaged of mouse pointed area
int srcx = (PartailMagImageView.Location.X + PartailMagImageView.Width / 2);
int srcy = (PartailMagImageView.Location.Y + PartailMagImageView.Height / 2);
e.Graphics.DrawImage(this.Image
, new System.Drawing.Rectangle(0, 0, PartailMagImageView.Width, PartailMagImageView.Height)
, new System.Drawing.Rectangle(srcx - ZoomOutRate / 2, srcy - ZoomOutRate / 2, ZoomOutRate, ZoomOutRate)
, GraphicsUnit.Pixel);
Pen mypen = new Pen(Color.Black, 7);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
e.Graphics.DrawEllipse(mypen, 0, 0, PartailMagImageView.Width - 7, PartailMagImageView.Height - 7);
if any one need to see complete code sample. i am attaching sample in dropbox link
https://www.dropbox.com/s/mogmshuiimtvhk7/ImageMagnifyingWindowsForm.zip?dl=0
PictureEdit has the ViewportToImage and ImageToViewport methods allowing you to convert viewport coordinates to the source image coordinates and vice versa.

Image original size WPF .NET

If I open an image using this code I get the correct width and height:
Bitmap bitmap = new Bitmap(#"C:\Users\Javier Escribano\Desktop\sample.png");
var imageHeight = bitmap.Height; //1270
var imageWidth = bitmap.Width; //1650
but If I use this code to show an image on WPF control the image is automatically resized. I want to keep the original dimensions:
ImageSource img = (ImageSource)new ImageSourceConverter().ConvertFromString(
#"C:\Users\Javier Escribano\Desktop\sample.png");
this.image.Source = img;
this.image.Width = img.Height; //1057
this.image.Height = img.Width; // 817
This is very likely caused by a mismatch of ppi (pixels per inch) of the png image and the dpi (dots per inch) of WPF. Scott Hanselman has a good post about it here.

textArea growByContent doesnt work in layer layout

I used layer layout to keep the title ie textArea on top of button image. But i cannot grow textArea size. Also the padding and margin on the textArea is not working as well.
Image btnIcon = URLImage.createToStorage(placeholder, "homeIcon" + imageUrl, allUrl.globalHomeImageUrl + imageUrl, URLImage.RESIZE_SCALE);
int width = placeholder.getWidth();
int height = placeholder.getHeight();
homeButton.setPreferredSize(new Dimension(width, height));
homeButton.getAllStyles().setBgImage(btnIcon);
TextArea buttonTitle = new TextArea(title);
buttonTitle.setRows(1);
buttonTitle.setUIID("smallLabel");
buttonTitle.getAllStyles().setPadding(0, 0, 0, 0);
buttonTitle.getAllStyles().setMargin(0, 4, 0, 4);
buttonTitle.setPreferredW(screenWidth / 3 - 30);
buttonTitle.setEditable(false);
buttonTitle.setGrowByContent(true);
buttonTitle.setIsScrollVisible(false);
buttonTitle.setGrowLimit(2);
Container container1 = new Container();
container1.setUIID("buttonTitleContainer");
container1.getAllStyles().setMargin(0, 5, 0, 0);
container1.getAllStyles().setBgTransparency(0);
container1.add(LayeredLayout.encloseIn(homeButton, FlowLayout.encloseRightBottom(buttonTitle)));
middleContainer.addComponent(BorderLayout.CENTER, container1);
How can i grow the textArea row as required and set the padding margin of the textarea.
Update:
i commented setPreferredW() and added calcPreferredSize() to set the dimension but how can i set the height dynamically??
TextArea buttonTitle = new TextArea(title){
#Override
protected Dimension calcPreferredSize() {
Dimension d = new Dimension(screenWidth/3-10, 40);
return d; //To change body of generated methods, choose Tools | Templates.
}
};
buttonTitle.setUIID("smallLabel");
buttonTitle.getAllStyles().setAlignment(Label.LEFT);
buttonTitle.setRows(1);
buttonTitle.getAllStyles().setPadding(0, 0, 0, 0);
buttonTitle.getAllStyles().setMargin(0, 0, 0, 4);
// buttonTitle.setPreferredW(screenWidth / 3 - 30);
buttonTitle.setEditable(false);
buttonTitle.setGrowByContent(true);
buttonTitle.setIsScrollVisible(false);
buttonTitle.setGrowLimit(2);
buttonTitle.setScrollVisible(false);
the design is somewhat like below:
Current screenshot:
As you can see, there is only one word displayed and the textArea always has only 1 row and doesnt grow
The problem isn't in the LayeredLayout it's due to the fact that you called the deprecated setPreferredW.
Notice that setPreferredW also limits the height.

kineticjs resize layer based on stage width

I have a stage that resizes to the container width based on browser width (trying to make my project responsive but struggling). I only have one layer and want the layer to resize based on the size of the stage. As of now the stage width changes with browser width but the layer width doesn't change, just gets cut off
I've tried layer.setScale(x,y) in a window.onresize function but don't have enough math sense to use this to scale the layer as the stage width changes.
I've also tried layer.setWidth(x) and used the stage width as a variable for x in the same function but no luck.
Shouldn't the layer automatically scale with the stage? If I adjust the canvas size in firebug the layer auto scales, but that's the only time I get the correct response...
By the way all the elements are polygons whose coordinates/points/etc are imported via svg/illustrator. Not sure if that has anything to do with the prob...
Totally stuck...
Here's my project: http://cityunite.org/new
There are many ways to approach this problem. I'll just give my suggestion.
You should not set the width of the stage when the browser window size changes, but you should scale instead. When you first load the page, your scale is set at 1, which is (100%). When your window size changes, you should listen for that change and set the scale accordingly.
Something like this:
//save initial scale
var initialScale = stage.scale(); //returns {x: 1, y: 1}
var initialWidth = $("#container").innerWidth(); // initial width
var initialHeight = $("#container").innerHeight(); // initial height
window.onresize = function(event) { // listen for change
var width = $("#container").innerWidth(); // new width of page
var height = $("#container").innerHeight(); // new height of page
console.log(width);
console.log(height);
var xScale = (width / initialWidth) * initialScale.x; // percent change in width (Ex: 1000 - 400/1000 means the page scaled down 60%, you should play with this to get wanted results)
var yScale = (height / initialHeight) * initialScale.y;
var newScale = {x: xScale, y: yScale};
console.log(newScale);
stage.setAttr('width', width);
stage.setAttr('height', height);
stage.setAttr('scale', newScale );
stage.draw();
}

Silverlight, calculate shape Height & Width using persentage

I have a silverlight application that works but I now need to resize some items using a percentage instead of using fixed Height and Width.
My code snippit for this is:
private void drawCell(int row, int col, string label, Color fill)
{
Rectangle rect = new Rectangle();
rect.Height = cellSize;
rect.Width = cellSize;
rect.StrokeThickness = 2;
rect.Stroke = new SolidColorBrush(Colors.DarkGray);
rect.Fill = new SolidColorBrush(fill);
Grid.Children.Add(rect);
Canvas.SetLeft(rect, col * cellSize);
Canvas.SetTop(rect, row * cellSize);
Rectangle innertec = new Rectangle();
innertec.Height = cellSize - 30;
innertec.Width = cellSize - 10;
innertec.StrokeThickness = 1;
innertec.Stroke = new SolidColorBrush(Colors.Black);
innertec.Fill = new SolidColorBrush(fill);
innertec.Margin = new Thickness(5);
Grid.Children.Add(innertec);
Canvas.SetLeft(innertec, col * cellSize);
Canvas.SetTop(innertec, row * cellSize);
Border productLabelBorder = new Border();
TextBlock productLabel = new TextBlock();
productLabel.Height = cellSize - 60;
productLabel.Width = cellSize - 10;
productLabel.Margin = new Thickness(5, innertec.Height + 5, 0, 5);
productLabel.TextAlignment = TextAlignment.Center;
productLabel.TextWrapping = TextWrapping.NoWrap;
productLabel.TextTrimming = TextTrimming.WordEllipsis;
productLabel.Text = label;
ToolTipService.SetToolTip(productLabel, label);
productLabelBorder.Child = productLabel;
Grid.Children.Add(productLabelBorder);
Canvas.SetLeft(productLabelBorder, col * cellSize);
Canvas.SetTop(productLabelBorder, row * cellSize);
}
What I want the code to do is take the innertec and productLabel, and calculate the Height and Width by first looking at the cellSize (which is a variable set elsewhere) and then create these objects with a percentage of cellSize.
The reason for this is that cellSize changed size depending on a slider in the UI. The "cells" resize after calculating the area of the Canvas.
Is it possible to calculate these values as a percentage?
Thanks in advance for your help.
It looks like you are not using your Grid correctly! I can see you are adding items to the Grid, then trying to position them via the Canvas attached properties. You are mixing two different panel types!
With the Canvas, children are positions by their Top and Left coordinates
With the Grid children are located within cells as indicated by the Grid.Row and Grid.Column attached property.
You can use 'star' widths and heights to define the proportional width and height of rows / columns in order have grid cells which are some percentage of the overall grid. For example in markup, if you want to add a grid cell which is 50% of the overall grid size you can do the following:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions
<Button Grid.Column="1" Content="foo"/>
</Grid>
With the above, the Button will have a width which is 50% of the overall Grid width.

Resources