I am trying to set the width of an image dynamically using MonoTouch. I have added the image to the view using Xcode Interface Builder.
What I want to do is keep the X and Y values of the image exactly the same. I just want the width of the image to change depending on a float value.
Example: If I placed the the image on the view at X = 50 and Y = 50 and the image size is W = 100 and H = 100 then I want to change the width dynamically based on conditions. I have tried to set the images' width dynamically like this:
img.Bounds.Width = 150;
and like this
img.Bounds.Size.Width = 150;
I have also tried to create a new RectangleF and setting the bounds equal to that Rectangle like this
RectangleF fillrect = new RectangleF(50, 50,
150, img.Bounds.Height);
and then setting the image bounds to that rectangle like this:
img.Bounds = fillrect;
The above method sizes the image but then moves the image to the incorrect place (X and Y values).
I have also tried various methods using SizeF but to no avail.
How can I just set the width of the image and keep it in the same place I placed it on interface builder?
And also how to change the X and Y values so that I can move another image based on the growth of the aforementioned image.
Seems like changing of Bounds applies around the Center of the view.
Try to set new Frame instead:
img.Frame = fillrect;
Location will be the same, but Size will be changed.
Related
I'm trying to get the size (width/height) of an Image control after I applied a Transform to it (either Render- or Layouttransform, doesn't matter in this case).
My Image control is embedded in a ScrollViewer. I'm using the Layout/Rendertransform to zoom the image which does work, but the ActualWidth/ActualHeight or RenderSize properties don't show me the new height/width. I tried calling InvalidateMeasure()and UpdateLayout() on the Image control without success.
This is my current code to zoom the image to the height:
double imageHeight = imageBox.ActualHeight;
double containerHeight = (imageBox.Parent as ScrollViewer).ActualHeight;
double facHeight = containerHeight / imageHeight;
var scaleTransform = imageBox.LayoutTransform.Value; //imageBox.RenderTransform.Value;
scaleTransform.ScalePrepend(facHeight, facHeight);
MatrixTransform newTransform = new MatrixTransform(scaleTransform);
imageBox.LayoutTransform = newTransform;
On first execution of that method the image will be (more or less) correctly zoomed. In this case, I want the image vertically fully shown, regardless of it's width, so that I only have a horizontal scrollbar (or no scrollbar at all if imageHeight > imageWidth).
On second execution, it'll zoom in again because imageBox.ActualHeight didn't change.
I tried various combinations of InvalidateMeasure and UpdateLayout but that didn't help.
For the transformed size after LayoutTransform (but not RenderTransform) was applied, check the Image control's DesiredSize property:
double imageHeight = imageBox.DesiredSize.Height;
I am trying to make a neat little bar graph with unique markers on the columns.
I used a 20x20px png image for this purpose and set it with
chart1.Series[0].Points[chart1.Series[0].Points.Count - 1].MarkerImage = imageIWanToUse;
but the marker is huge, tried to use
chart1.Series[0].Points[chart1.Series[0].Points.Count - 1].MarkerSize = 1;
but with no luck, i get this on all of the columns, where can i fix this?
image of a column
You need to make sure that the image you use has the proper dpi setting, so that it fits with your screen's dp, which probably is around 75-96 dpi.
If it looks too large than the dpi of the image is too small..
You can change it in code like this:
Bitmap bmp = (Bitmap) Bitmap.FromFile("D:\\stop32.png");
bmp.SetResolution(50, 50);
bmp.Save("D:\\stop32_50dpi.png");
bmp.SetResolution(250, 250);
bmp.Save("D:\\stop32_250dpi.png");
Series S0 = chart1.Series[0];
S0.Points[chart1.Series[0].Points.Count - 3].MarkerImage = "D:\\stop32.png";
S0.Points[chart1.Series[0].Points.Count - 2].MarkerImage = "D:\\stop32_50.png";
S0.Points[chart1.Series[0].Points.Count - 1].MarkerImage = "D:\\stop32_250.png";
Here are the resulting markers:
The original resolution was 96dpi. (Left marker.)
You will have to watch out for varying screen dpi and use either different images or create the right one dynamically. You can get the current screen dpi e.g. by testing the current Graphics object in a Paint event: Console.WriteLine(e.Graphics.DpiX + " dpi x"); For my screen this resulted in 120 dpi..
I load pdfdoc, by:
PdfReader pdfReader = new PdfReader(byteArray);
LocationTextExtractionStrategyEx st3 = new LocationTextExtractionStrategyEx();
PdfTextExtractor.GetTextFromPage(pdfReader, 1, st3);
Now I can get list of page elements from st3.TextLocationInfo. Every element has property TopLeft and BottomRight, they are Vector.
How can I get element position if I don't know max value of scale. I know that vector start on left bottom page corner but I don't know where is end because I don't know page size in the same scale like vector.
I can run
var pageSize = pdfReader.GetPageSize(1)
But values from vectors are bigger than pageSize Width and Height
On the other hand, can I load every char position on page?
LocationTextExtractionStrategyEx is not part of iTextSharp. I assume, therefore, you talk about the class provided in this answer. That class does nothing fancy with the positions. Thus, to respond to your issue:
I know that vector start on left bottom page corner but I don't know where is end because I don't know page size in the same scale like vector.
I can run
var pageSize = pdfReader.GetPageSize(1)
But values from vectors are bigger than pageSize Width and Height
First of all: the coordinates you get from LocationTextExtractionStrategyEx.TextLocationInfo indeed are to be interpreted in the context of pdfReader.GetPageSize.
There are two major causes why the vector values can be beyond Width and Height of the latter:
The rectangle returned by pdfReader.GetPageSize does not need to be based in (0,0). It could e.g. have x coordinates in 301..400 and y coordinates in 501..600. In that case height and width would both be 100 but all coordinates of points in that rectangle would be higher.
Thus, do not look at Width and Height but instead at Left, Bottom, Right, and Top.
Text may actually be outside the visible page and, therefore, have coordinates outside of pdfReader.GetPageSize.
For a final verdict please supply the PDF in question.
I read page size by
var pageSize = pdfReader.GetPageSize(1)
next I created
TextInfoLocation textLocation = new TextInfoLocation(textLine.TopLeft, textLine.BottomRight, this.PdfFilePageSize);
Properties .TopLeft and .BottomRight are vectors.
textLine is LocationTextExtractionStrategyEx.TextInfo object read from pdfReader by strategy.
Now text position in pixels form vectores I can get from:
double leftMargin = textLocation.LeftMargin;
I have a WinForms empty panel, and I'm adding images dynamically to this panel.
To center the first image added, I just:
Get the width (WP) of the panel and divide by 2;
Get the width (WI) of the image and divide by 2;
WP - WI = Left position of image X;
But I can't figure out some dinamically way to set this position when I have more than one image. Is there a way to calculate this X ? Am I right about this? Is there a easier way?
Thanks.
Well if you have only one row, some where you can have a collection of Image objects, say
List<Image> images.
And method
void PositionImages()
{
int totalWidth = images.Sum(img=>img.Width);
int startX = (panel.Width - totalWidth)/2;
}
Should work to you, but you will need to check it. For example I suppose here that tolalWidth of all images in collection is always less then panel.Width.
I have a ListView with View property set to LargeIcon. And I have an ImageList as image source for ListView.
What I want is to display both vertically and horizontally oriented images in ListView but ImageList have only a single property ImageSize for all images in its collection so for example if I set that property to 150x100 and add a vertical image (100x150) to collection - ListView automatically stretches it to 150x100.
So as I understand I need some ImageList, in which every image is stored with its original size. Any thoughts about how to do that?
Thanks in advance.
I have had this problem myself, Here is what I did, I hope it will help you as well, first determine the biggest image size you would use ( for example 200x200) then use Png or Gif images (all 200x200) with transparent backgrounds.
have a look at these two images i have created as an example.
but I make it like this to avoid stretching:
To keep from stretching you can use cross-multiplication.
Image Width Desired Width
--------------- = ---------------
Image Height New Height
or
Image Width New Width
--------------- = ---------------
Image Height Desired Height
In code it looks something like this
int height = img.Width*newWidth/image.Height;
or
int height = img.Width/image.Height*newWidth;
and
int width = img.Height*newHeight/image.Width;
or
int width = img.Height/image.Width*newHeight;
Then when creating your new bitmap you can draw a scaled image on the new bitmap using the desired size and the derived size.