I'am working on a app where the user can put in some data and then take a image with the camera or from the device.
The problem i'am having is that i cant seem to get a cell or element to only display a image.
Width of the image would be the width of the cell(Grouped style) it's in. Height i would like to specify myself.
I've tried to subclass ImageElement but can't change the size of it.
Is the already a Element i could use for this or is there any other good way of doing it?
Try to use the UIViewElement with an UIImageView:
var imageView = new UIImageView(new RectangleF(0, 0, 300, 100));
imageView.ContentMode = UIViewContentMode.Center;
var element = new UIViewElement(String.Empty, imageView, true);
Related
I would like to be able to fill graphics objects using the tiling pattern functionality that pdf offers. For example, I would like to be able to draw something like this:
iText7 has a few objects related to patterns that could be useful, but I am having trouble figuring out how to use them and it is exceedingly difficult to find examples of similar code online.
iText7 provides the following classes that may be useful:
PdfPattern.Tiling
PatternColor
PdfPatternCanvas
It looks like you should be able to create a PdfPattern.Tiling object which references an image in some way and then create a PatternColor from that tiling object. Then you can set your canvas' fill color to the PatternColor you just created. An example of a function that does this is:
private void SetImageTilingFill(PdfCanvas canvas, PdfImageXObject img)
{
PdfPattern.Tiling tiling = new PdfPattern.Tiling((float)Inches2Points(img.GetHeight() / 96), (float)Inches2Points(img.GetWidth() / 96)); // create tiling object with width and height the size of the img
tiling.GetResources().AddImage(img);// add the image as a resource?
canvas.SetFillColor(new PatternColor(tiling)); // set fill color to PatternColor?
}
So far this approach has not been successful, my rectangle ends up solid black. Any suggestions would be much appreciated.
Using an image as a tile requires setting up the tile (it has a canvas to draw on). And then using it as a fill when drawing.
try (PdfWriter writer = new PdfWriter("tiling.pdf");
PdfDocument document = new PdfDocument(writer)) {
// creating image
ImageData imageData = ImageDataFactory.create("wavy.png");
PdfImageXObject imageXo = new PdfImageXObject(imageData);
// setting up the tile
PdfPattern.Tiling imageTile = new PdfPattern.Tiling(100, 100);
new PdfPatternCanvas(imageTile, document)
.addXObjectFittedIntoRectangle(imageXo, new Rectangle(0, 0, 100, 100))
.release();
PdfPage page = document.addNewPage();
PdfCanvas canvas = new PdfCanvas(page);
//using the tile
canvas.setFillColor(new PatternColor(imageTile));
canvas.rectangle(10, 10, 500, 900).fill();
canvas.release();
}
I'm having trouble setting the padding for a button to center the content (text + drawable).
Here is where I've gone so far:
Button done = new Button(this)
{
Text = GetText((int)typeof(Resource.String).GetField("goalreached").GetValue(null))
};
done.LayoutParameters = doneButtonParams;
done.SetBackgroundResource(Resource.Drawable.DoneGreenYakaButton);
done.Typeface = Typeface.CreateFromAsset(Assets, "fonts/Ubuntu-Medium.ttf");
done.SetTextColor(Color.White);
done.SetTextSize(Android.Util.ComplexUnitType.Sp, 12);
doneButtonParams.ColumnSpec = GridLayout.InvokeSpec(0, 2); // Setting colspan = 2
var check = GetDrawable(Resource.Drawable.ic_check_16px);
check.SetBounds(0, 0, 32, 32); // Set the image size
check.SetTint(Color.White); // Set the image color
done.SetCompoundDrawables(null, null, check, null);
The result is this:
Obviously I want the check mark to be nearer the text and have the same padding as the one for the text on the left of the button.
What Am I missing here ?
Thanks in advance.
I'm not familiar with xamarin for mobile development but the native Android way of doing this is setting the drawable padding on the button itself. setCompoundDrawablePadding(int) is the native method to accomplish adjusting the distance between text and the drawable.
Ex:
done.setCompoundDrawablePadding(15);
Edit: I realized you are looking to add padding on the outside of the checkmark, this would be accomplished with plain old setPadding(int, int, int, int).
Ex:
setPadding(0,0,15,0);
i have an dataGridView which is inside of an TabPage. Now i need to create an image of the dataGridView, but i looked online for solutions and i found DrawToBitmap but that only takes a part of the dataGridView.
This is the code i am using right now
bm = new Bitmap(this.dataGridView2.Width, this.dataGridView2.Height);
dataGridView2.DrawToBitmap(bm,new Rectangle(100,100, his.dataGridView1.Width, this.dataGridView1.Height));
e.Graphics.DrawImage(bm, 0, 0);
Here is an example how to fit a DataGridView onto a Bitmap, even if its content is larger than the DataGridView or the TabPage or even the Form.
The trick is to temporarily enlarge the DataGridView for the DrawToBitmap call so that all cells fit in and no scrollbars are there. Afterwards set it back to the original size that fits your layout..:
Size oldsize = dataGridView1.ClientSize;
var tw = dataGridView1.Columns.Cast<DataGridViewColumn>().Select(x => x.Width).Sum();
var th = dataGridView1.Rows.Cast<DataGridViewRow>().Select(x => x.Height).Sum();
dataGridView1.ClientSize = new Size(tw + dataGridView1.RowHeadersWidth,
th + dataGridView1.ColumnHeadersHeight);
Bitmap bmp = new Bitmap(dataGridView1.ClientSize.Width, dataGridView1.ClientSize.Height);
using (Graphics G = Graphics.FromImage(bmp))
dataGridView1.DrawToBitmap(bmp, dataGridView1.ClientRectangle);
bmp.Save(yourFilename, ImageFormat.Png);
dataGridView1.ClientSize = oldsize;
If you want padding around it you can simply make the Bitmap even larger still and write out the target rectangle's coordinates explicitly, instead of using the ClientRectangle..
I have several paragraphs of text and couple of pictures between these paragraphs.
Now, I want to generate a picture using these materials, merging them vertically. But all the blocks of the text and pictures can not have bigger width than that of the generating picture, which means I have to zoom out the origin pictures, and fill each paragraph of text into a rectangle to fit the width.
Here is the tough thing:
To figure out the size of the rectangle to contain the text, I need use Graphics.MeasureString() method, which needs an instance of Graphics used to generate my picture(now, I'm using a blank template picture). But I do not know the exact size of this Graphics until I figure out all the sizes of rectangles and pictures.
Is there any method to get an instance of Graphics without source image?
Or is there any other method to do this work?
hope this could help dude .
http://chiragrdarji.wordpress.com/2008/05/09/generate-image-from-text-using-c-or-convert-text-in-to-image-using-c/
https://web.archive.org/web/20131231000000/http://tech.pro/tutorial/654/csharp-snippet-tutorial-how-to-draw-text-on-an-image
http://www.codeproject.com/Questions/388845/HOW-TO-MAKE-HIGH-QAULITY-IMAGE-WITH-TEXT-IN-Csharp
thank you
For people how are intrested in a WPF solution (as asked):
public static BitmapSource CreateImage(string text, double width, double heigth)
{
// create WPF control
var size = new Size(width, heigth);
var stackPanel = new StackPanel();
var header = new TextBlock();
header.Text = "Header";
header.FontWeight = FontWeights.Bold;
var content = new TextBlock();
content.TextWrapping = TextWrapping.Wrap;
content.Text = text;
stackPanel.Children.Add(header);
stackPanel.Children.Add(content);
// process layouting
stackPanel.Measure(size);
stackPanel.Arrange(new Rect(size));
// Render control to an image
RenderTargetBitmap rtb = new RenderTargetBitmap((int)stackPanel.ActualWidth, (int)stackPanel.ActualHeight, 96, 96, PixelFormats.Pbgra32);
rtb.Render(stackPanel);
return rtb;
}
is there a way to change size of chart when using method Chart.SaveImage() from the source code?
Right now the only way I found to set the size of chart, is resize the form on which chart control (System.Windows.Forms.DataVisualization.Charting.Chart) sits. Can I explicit set its width and height? Trying to change Chart.Size, Chart.Width or Chart.Size doesn't work.
All right. The solution was so obvious that I couldn't found it thou 3 days - I had setted Chart.Dock = DockStyle.Fill, so changing Size property doesn't affect. After modified it to DockStyle.None I could change chart's size and (finally!) save it with appropriative width and height.
You can define it by redefining the Size property of the chart :
var ch = new Chart();
ch.Size = new Size(600, 250);
You'll probably have to save it to a memory stream, then use the Image class to change dimensions and then save it to file.
using(MemoryStream ms = new MemoryStream(4096))
{
myChart.SaveImage(ms,ImageFormat.Png);
using(Bitmap img = Image.FromStream(ms))
{
using(Graphics g = Graphics.FromImage(img))
g.DrawImage( b, 0, 0, newWidth, newHeight );
}
img.Save("where\to\save\chart.png",ImageFormat.Png);
}
}