CGRect sublayer background didn't centered XAMARIN.IOS - layout

I'm trying to use the CGRect class to create a sublayer for a Stack View object, the intention is to create a colored background with rounded edges to make it more pleasing to the eye. As in the picture:
But as you can see both the green and blue boxes are not centered with the stack views, while the stack views in the xamarin layout are centered, I create the rectangles as follows:
var bg = new UIView();
var AStack = new UIView();
var MStack = new UIView();
var SubInfoStack = new UIView();
var GreenColor = UIColor.FromRGB(80, 220, 100);
var SubColor = UIColor.FromRGB(123, 168, 202);
//CGrect funziona attorno all'oggetto
bg.Frame = new CGRect(-5, -10, View.Bounds.Width-30, 100);//stackPlico.Bounds;
//Nel caso ci fossero vincoli a livello orizonatale devi tenerne conto sia nella coordinata di origine (x) che in quella di lunghezza anchessa (x)
AStack.Frame = new CGRect(15,-30, AltezzaStack.Bounds.Width-15, AltezzaStack.Bounds.Height+60);
MStack.Frame = new CGRect(MassaStack.CoordinateSpace.Bounds.X + 10, -30, MassaStack.Bounds.Width-10, MassaStack.Bounds.Height+60);
SubInfoStack.Frame = new CGRect(StackVerticalSubMisure.Layer.Bounds.Left, StackVerticalSubMisure.Layer.Bounds.Top-20, StackVerticalSubMisure.Layer.Bounds.Right+20, StackVerticalSubMisure.Layer.Bounds.Height+40);
//Coloro i layer
bg.BackgroundColor = UIColor.TertiarySystemGroupedBackgroundColor;
AStack.BackgroundColor = GreenColor;
MStack.BackgroundColor = GreenColor;
SubInfoStack.BackgroundColor = SubColor;
//Coloro i testi
AltezzaLabel.TextColor = UIColor.White;
MassaLabel.TextColor = UIColor.White;
VitaL.TextColor = UIColor.White;
BracciaL.TextColor = UIColor.White;
PolpacciL.TextColor = UIColor.White;
QuadricipitiL.TextColor = UIColor.White;
ToraceL.TextColor = UIColor.White;
//Arrotondo i layer
bg.Layer.CornerRadius = 10;
AStack.Layer.CornerRadius = 50;
MStack.Layer.CornerRadius = 50;
SubInfoStack.Layer.CornerRadius = 50;
//Gli aggiungo agli oggetti di layout
stackPlico.InsertSubview(bg, 0);
AltezzaStack.InsertSubview(AStack, 0);
MassaStack.InsertSubview(MStack, 0);
StackVerticalSubMisure.InsertSubview(SubInfoStack, 0);
I've tried with all the methods I could know to get the exact coordinates and the length of the stack in question to build them around the layer with the background but nothing, when I can center it for one device (iPhone XR) for another (iPhone 8) the frame moves and does not stay centered in the viewcontroller.

Related

UIScrollView is stopping too soon

I'm using a scrollview to make a image gallery for my app and I have it mostly working. it'll allow me to scroll through the images one by one but the very last image always get cut off and I'm not sure why.
this is the bulk of the operation:
var idx = 0;
foreach (var mediaItem in _mediaItems)
{
var xPosition = UIScreen.MainScreen.Bounds.Width * idx;
var imageView = new UIImageView();
imageView.SetImage(new NSUrl(mediaItem), UIImage.FromBundle("image_placeholder"));
imageView.Frame = new CGRect(xPosition, 0, svGallery.Frame.Width + 50, svGallery.Frame.Height);
imageView.ContentMode = UIViewContentMode.ScaleAspectFit;
svGallery.ContentSize = new CGSize
{
Width = svGallery.Frame.Width * (idx + 1)
};
svGallery.AddSubview(imageView);
idx++;
}
minus that flaw, this works perfectly and as I expect it to.
From shared code , the Width of ContenSize is:
Width = svGallery.Frame.Width * (idx + 1)
However, each Width(svGallery.Frame.Width + 50) of ImageView is greater than vGallery.Frame.Width:
imageView.Frame = new CGRect(xPosition, 0, svGallery.Frame.Width + 50, svGallery.Frame.Height);
Therefore, the actually Width of ContenSize can not contains all the ImageView's Content. And if the number of ImageView is larger, the last picture will be cut off more.
You can modif the Width of ContentSize as follow to check whether it works:
svGallery.ContentSize = new CGSize
{
Width = (svGallery.Frame.Width + 50) * (idx + 1)
};

How can I make an image used on multiple Excel spreadsheets always display at its full size (Aspose Cells)?

I am using the following code to place an image on a spreadsheet:
var ms = new MemoryStream();
Image _logo = RoboReporterConstsAndUtils.GetURLImage("http://www.proactusa.com/bla/pa_logo_notag.png");
_logo.Save(ms, ImageFormat.Png);
ms.Position = 0;
locationWorksheet.Pictures.Add(0, 4, ms);
AutoFitterOptions options = new AutoFitterOptions { OnlyAuto = true };
locationWorksheet.AutoFitRows(options);
It works fine; however, I use this same code on two different reports, and the image displays at different sizes. On one it has a height of 0.85" (63%) and a width of 1.1" (53%), while on the other it has a height of 1.44" (106%) and a width of 2.07" (100%).
Why would they differ in size? And why wouldn't they be 100% of the original image size?
The other code, which seems to be exactly the same (although in this case the column at which the image appears is dynamic), is:
var ms = new MemoryStream();
Image _logo = RoboReporterConstsAndUtils.GetURLImage("http://www.proactusa.com/bla/pa_logo_notag.png");
_logo.Save(ms, ImageFormat.Png);
ms.Position = 0;
pivotTableSheet.Pictures.Add(0, _grandTotalsColumnPivotTable - 1, ms);
AutoFitterOptions options = new AutoFitterOptions { OnlyAuto = true };
pivotTableSheet.AutoFitRows(options);
The image itself, at the location referenced, has a height of 1.35" and a width of 2.07"
The method called is:
internal static Image GetURLImage(string url)
{
WebClient wc = new WebClient();
byte[] bytes = wc.DownloadData(url);
MemoryStream ms = new MemoryStream(bytes);
return Image.FromStream(ms);
}
How can I get the image to always display at 100%, or at least at a given size?
UPDATE
I also have (at least for now) some reports in the same project that are generated using EPPlus. In these, I have the following code, which allows me to set the exact size of the image:
private void AddImage(ExcelWorksheet oSheet, int rowIndex, int colIndex)
{
Image _logo = RoboReporterConstsAndUtils.GetURLImage("http://www.proactusa.com/bla/pa_logo_notag.png");
var excelImage = oSheet.Drawings.AddPicture("PRO*ACT Logo", _logo);
excelImage.From.Column = colIndex - 1;
excelImage.From.Row = rowIndex - 1;
excelImage.SetSize(199, 130); // 199WX130H is the actual size of the image
excelImage.From.ColumnOff = Pixel2MTU(2);
excelImage.From.RowOff = Pixel2MTU(2);
}
...which is called like so:
AddImage(deliveryPerformanceWorksheet, UNIT_ROW, LOGO_FIRST_COLUMN);
...but this won't fly in the Aspose code, because the sheet is of a different type - an Aspose.Cells.Worksheet instead of an ExcelWorksheet, and thus this code:
AddImage(locationWorksheet, 0, 4);
... won't compile in the Aspose report. I wish I could temporarily convert the Aspose.Cells.Worksheet to an ExcelWorksheet as cavalierly as this:
ExcelWorksheet ews = locationWorksheet; // naive attempt to magically morph an Aspose.Cells.Worksheet to an ExcelWorksheet
AddImage(ews, 0, 4);
...so that I could call AddImage(), but that flagrant attempt is tweeted to a halt by the compiler whistling, "Cannot implicitly convert type 'Aspose.Cells.Worksheet' to 'OfficeOpenXml.ExcelWorksheet'"
UPDATE 2
The image is the expected size; this code:
int h = _logo.Height; //130, as expected
int w = _logo.Width; //199, " "
...showed the image was the original size. Could the problem be the AutoFitterOptions setting? Does OnlyAuto allow stretching/squashing of the image, depending on the size of the cell into which it is plopped?
UPDATE 3
In EPPlus I can get the images to display at exactly the same size using this code:
private void AddImage(ExcelWorksheet oSheet, int rowIndex, int colIndex)
{
Image _logo = RoboReporterConstsAndUtils.GetURLImage("http://www.proactusa.com/bla/pa_logo_notag.png");
var excelImage = oSheet.Drawings.AddPicture("PRO*ACT Logo", _logo);
excelImage.From.Column = colIndex - 2;
excelImage.From.Row = rowIndex - 1;
excelImage.SetSize(199, 130);
excelImage.From.ColumnOff = Pixel2MTU(2);
excelImage.From.RowOff = Pixel2MTU(2);
}
...but in Aspose I can only come close using:
var ms = new MemoryStream();
Image _logo = RoboReporterConstsAndUtils.GetURLImage("http://www.proactusa.com/bla/pa_logo_notag.png");
_logo.Save(ms, ImageFormat.Png);
ms.Position = 0;
pivotTableSheet.Pictures.Add(0, _grandTotalsColumnPivotTable - 1, ms);
And the EPPlus code also retains the height/width ratio:
The original image is 199 pixels wide and 130 pixels high.
The EPPlus-plopped images are 1.33 X 2.05, so the ratio of 1.5:1 (close approximation) is retained.
The Aspose-plopped images, though, are 1.63 and 1.67 X 2.07, so the ratio is more like 1.25:1
So even with the AutoFitter jazz commented out of the Aspose code, the image still gets either squashed in width or stretched in height.
UPDATE 4
Based on a thread here, I tried this (afer copying the image to my bin folder):
int index = locationWorksheet.Pictures.Add(0, 4, 6, 5, "LogoFromSite.png");
Picture pic = locationWorksheet.Pictures[index];
pic.Placement = PlacementType.FreeFloating;
The first four arguments to [sheet].Pictures.Add() are Upper Left Row, Upper Left Column, Lower Right Row, and Lower Right Column.
However, this puts the image on the page in the right place, but then moves it to the left several columns (!?!)
UPDATE 5
I found another ray of hope here, and tried this code:
Aspose.Cells.Rendering.ImageOrPrintOptions opts = new Aspose.Cells.Rendering.ImageOrPrintOptions();
opts.OnePagePerSheet = true;
opts.ImageFormat = ImageFormat.Png;
opts.SetDesiredSize(199, 130);
Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(locationWorksheet, opts);
sr.ToImage(0, "LogoFromSite.png");
...but got this:
So: squashed again.
UPDATE 6
I tried some code provided by the Aspose Cells cats themselves, but they admitted there was a problem with it, and were looking into it. Just for grins, I gave it a shot to see what would transpire. This code:
byte[] bts1 = File.ReadAllBytes("LogoFromSite.png");
byte[] bts2 = File.ReadAllBytes("LogoFromSite.png");
MemoryStream ms1 = new MemoryStream();
ms1.Write(bts1, 0, bts1.Length);
ms1.Position = 0;
//This is for second picture in sheet2
MemoryStream ms2 = new MemoryStream();
ms2.Write(bts2, 0, bts2.Length);
ms2.Position = 0;
//Add picture in first worksheet
int idx = locationWorksheet.Pictures.Add(0, 4, ms1);
//Add picture in second worksheet with original size
idx = locationWorksheet.Pictures.Add(0, 10, ms2);
Picture pic = locationWorksheet.Pictures[idx];
pic.HeightScale = 100;
pic.WidthScale = 100;
...resulted in these "no image images":
UPDATE 7
I made another venture; as the height was being increased above and beyond 100%, I thought I would resize the image into another one, and use that:
var ms = new MemoryStream();
Image _logo = GetURLImage("http://www.proactusa.com/bla/pa_logo_notag.png");
double newHeightDbl = _logo.Height * 0.8;
int newHeightInt = (int)Math.Ceiling(newHeightDbl);
Image resizedImage = ResizeImage(_logo, newHeightInt, _logo.Width);
resizedImage.Save(ms, ImageFormat.Png);
ms.Position = 0;
locationWorksheet.Pictures.Add(0, 4, ms);
...but no! It stuffs the whole shebang into one measly column, like so:
...and gumbifies it galore vertically, thus making it look queasier than a lubber on a tempest-tossed tug.
Here is the (stolen/borrowed) code to resize the image:
// from http://stackoverflow.com/questions/1922040/resize-an-image-c-sharp
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
Please check your thread in Aspose.Cells forum which answers two of your following questions.
1 - Can we reuse same memory stream object containing picture in workbooks and worksheets?
2 - How to add picture with original size?
Note: I am working as Developer Evangelist at Aspose
Simply a matter of commenting out the fancy-pants autofitting code:
//AutoFitterOptions options = new AutoFitterOptions { OnlyAuto = true };
//pivotTableSheet.AutoFitRows(options);
Now the image is displayed uniformly at pretty much its actual size (but note the caveat below); a scosh "spilly" at times, but if they complain about that, I'll create a second image, and resize it using this:
// from http://stackoverflow.com/questions/1922040/resize-an-image-c-sharp
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
Caveat emptor: This works well enough that I am grudgingly accepting it, but the images placed on the sheet are not exactly the same size. One is 1.67" X 2.07", the other is 1.63" X 2.07" - close enough for horseshoes, hand-grenades, and images on Excel spreadsheets, I guess.

Javafx Text Property Binding

I am struggling to bind some text specified coordinates so that when I resize the window the text follows suit. Here is the portion of my code:
for (int i = 0; i < petrolStations.size() / 2; i++) {
int j = i + 1;
Text text1 = new Text(petrolStations.get(i), petrolStations.get(j), "1");
text1.setFont(Font.font("Courier", FontWeight.BOLD, FontPosture.ITALIC, 10));
text1.xProperty().bind(pane.widthProperty().divide(2));
text1.yProperty().bind(pane.heightProperty().divide(2));
pane.getChildren().add(text1);
To explain: petrolStations is an array of coordinates that are used to place a letter 1 on the page.
Here is the current output, as you can see all the 1's are combining in the middle rather than being in their specified coordinates.
EDIT:
I've changed the 1's to circles and managed to scale up the size but I still have the same problem, since all the coordinates are under 100 they sit up the top left, I need them to encompass the whole window and expand and separate as the window is resized larger.
for (int i = 0; i < petrolStations.size() / 2; i++) {
int j = i + 1;
Circle circle1 = new Circle();
circle1.setCenterX(petrolStations.get(i));
circle1.setCenterY(petrolStations.get(j));
circle1.setRadius(1);
circle1.setStroke(Color.BLACK);
circle1.setFill(Color.WHITE);
circle1.setScaleX(3);
circle1.setScaleY(3);
pane.getChildren().add(circle1);
}
http://i.imgur.com/JkV3LiW.png
Why not take the ratio of x and y with respect to the width/height? Take a look at this:
Pane pane = new Pane();
Text text = new Text(250,250,"1");
pane.getChildren().add(text);
double width = 150;
double height = 150;
Scene scene = new Scene(pane, width, height);
double x = 50;
double y = 50;
double xRatio = x / width; //GET THE RATIO
double yRatio = y / width; //GET THE RATIO
text.xProperty().bind(pane.widthProperty().multiply(xRatio));
text.yProperty().bind(pane.heightProperty().multiply(yRatio));
stage.setTitle("Hello World!");
stage.setScene(scene);
stage.show();
When I run this code, the initial state is:
Upon resizing:

Pixi.js how to render texture on polygon

I want to render a texture on polygon using pixi.js, sprites just offer me a square option. i need to change that (top, left), (top, right), (bottom, left), (bottom, right) in order to render a imagen inside it.
http://cl.ly/image/1s2Y2f331h0a/Screen%20Shot%202013-12-16%20at%203.11.02%20PM.png
how can i resolve this?
something like, beginFillBitmap or any other way?
Same here :) I'm investigating if pixi is suitable for my project and the last thing I need is rendering textures on non-rectangles.
Only thing I have found so far is masking:
http://www.goodboydigital.com/pixi-js-brings-canvas-and-webgl-masking/ . Maybe this can help you.
But it is not enough good for me. I need also ability to repeat texture in X and Y axis. Any other ideas? Or is there another js html5 framework that has that ability?
Use PIXI.mesh.Mesh with Pixi v4 or PIXI.SimpleMesh with Pixi v5.
This is a simple example that works with all kind of renderers (you can paste it in the Pixi Examples page https://pixijs.io/examples-v4/?v=release ):
var app = new PIXI.Application(800, 600, { backgroundColor: 0x1099bb });
document.body.appendChild(app.view);
// create 3 vertex 2D positions (x, y)
var meshv = new Float32Array(2*3);
var i = 0;
meshv[i++] = 0; meshv[i++] = 0;
meshv[i++] = 100; meshv[i++] = 0;
meshv[i++] = 100; meshv[i++] = 100;
// create 3 vertex texture coordinate UV positions (u,v)
var meshuv = new Float32Array(2*3);
i = 0;
meshuv[i++] = 0; meshuv[i++] = 0;
meshuv[i++] = 1; meshuv[i++] = 0;
meshuv[i++] = 1; meshuv[i++] = 1;
// create 1 triangle indexes (i1, i2, i3) referencing indexes in the other two arrays
var meshi = new Uint16Array(1*3);
i = 0;
meshi[i++] = 0; meshi[i++] = 1; meshi[i++] = 2;
// create the mesh object
var mesh = new PIXI.mesh.Mesh(
PIXI.Texture.fromImage('examples/assets/bunny.png'),
meshv, meshuv, meshi, PIXI.DRAW_MODES.TRIANGLES
);
// attach to the stage, container...
app.stage.addChild(mesh);
You can also use newer Pixi v5 features to a more advanced example like in https://pixijs.io/examples/#/mesh/triangle-textured.js but can be less compatible.
Edit: for wrapping modes (for repeating the texture) check http://pixijs.download/dev/docs/PIXI.BaseTexture.html wrap mode options.

How to create image from bitmap and save it

Here is sample code that I'm tryin to make work, but no luck so far.
Bitmap bitmap = new Bitmap((Stream)Cache["images"]);
Graphics g = Graphics.FromImage(bitmap);
StringFormat strFrmt = new StringFormat();
strFrmt.Alignment = StringAlignment.Center;
SolidBrush btmForeColor = new SolidBrush(Color.Green);
SolidBrush btmBackColor = new SolidBrush(Color.Black);
Font btmFont = new Font("Verdana",7);
SizeF textSize = new SizeF();
textSize = g.MeasureString("Copyright", btmFont);
float x = ((float) bitmap.Width - textSize.Width - 3);
float y = ((float) bitmap.Height - textSize.Height - 3);
float w = ((float) x + textSize.Width);
float h = ((float) y + textSize.Height);
RectangleF textArea = new RectangleF(x,y,w,h);
g.FillRectangle(btmBackColor,textArea);
g.DrawString("Copyright",btmFont,btmForeColor,textArea);
btmForeColor.Dispose();
btmBackColor.Dispose();
btmFont.Dispose();
g.Dispose();
As you can see from the code, I'm getin Stream, and creatin bitmap, then making some changes upon bitmap, and now I want to save my bitmap object, but can't figure out how, I made some research in internet, but all the examples/articles/forum posts were for cases when you have some image file on the server, and want to make changes, in my case, I just have some stream, and want to save bitmap object in specific path. How can I do this ? Any kind of help would be appreciated.
Surely its just bitmap.save? or one of its overloads? g is really drawing on the bitmap.
** update
Bitmap bitmap = new Bitmap(#"C:\Users\mike\Pictures\Panasonic\P1000016.jpg");
Graphics g = Graphics.FromImage(bitmap);
StringFormat strFrmt = new StringFormat();
strFrmt.Alignment = StringAlignment.Center;
SolidBrush btmForeColor = new SolidBrush(Color.Green);
SolidBrush btmBackColor = new SolidBrush(Color.Black);
Font btmFont = new Font("Verdana", 90);
SizeF textSize = new SizeF();
textSize = g.MeasureString("Copyright", btmFont);
float x = ((float)bitmap.Width - textSize.Width - 3);
float y = ((float)bitmap.Height - textSize.Height - 3);
float w = ((float)x + textSize.Width);
float h = ((float)y + textSize.Height);
RectangleF textArea = new RectangleF(x, y, w, h);
g.FillRectangle(btmBackColor, textArea);
g.DrawString("Copyright", btmFont, btmForeColor, textArea);
btmForeColor.Dispose();
btmBackColor.Dispose();
btmFont.Dispose();
g.Dispose();
bitmap.Save(#"C:\Users\mike\Pictures\Panasonic\P1000016_0.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
this works for me, had to increase font size though.
seems I found a solution
var photoPath = Server.MapPath("~/" + AsyncFileUpload1.FileName);
if (File.Exists(photoPath))
{
File.Delete(photoPath);
}
using (var mainFile = File.Create(photoPath))
{
// dostuff()
bitmap.Save(mainFile, image.RawFormat);
}
I had to create file before saving it. Thank you Mike for beeing with me in this, I'm really appreciated!!!

Resources