Saving a polygon object into a bitmapimage in WIndows phone 8.1 - image

I am working on a windows phone 8.1 app where I need to convert a polygon object to an image and ultimately save it as a png file. Till now, I created a polygon object with various properties. Now I'm clueless about the other parts.
pol.Opacity = 0.5;
System.Windows.Point Point1 = new System.Windows.Point(10, 200);
System.Windows.Point Point2 = new System.Windows.Point(60, 140);
System.Windows.Point Point3 = new System.Windows.Point(130, 140);
System.Windows.Point Point4 = new System.Windows.Point(180, 200);
System.Windows.Point Point5 = new System.Windows.Point(130, 260);
System.Windows.Point Point6 = new System.Windows.Point(60, 260);
PointCollection myPointCollection = new PointCollection();
myPointCollection.Add(Point1);
myPointCollection.Add(Point2);
myPointCollection.Add(Point3);
myPointCollection.Add(Point4);
myPointCollection.Add(Point5);
myPointCollection.Add(Point6);
pol.Points = myPointCollection;
var imageBrush = new ImageBrush();
imageBrush.ImageSource = image.Source;
pol.Fill = imageBrush;
pol.Height = image.Height;
pol.MaxHeight = image.Height;
pol.MaxWidth = image.Width;
pol.Width = image.Width;
pol.Stroke = new SolidColorBrush(Colors.Red);
pol.StrokeThickness = 2;
pol.Margin = image.Margin;

You can use the WritableBitmap class in order to achieve this. I have a similar post on Silverlight, which you can refer: How to Crop an Image based on a Shape or Path control?. Hope that helps, at least gives some basic concepts. Let me know, if you need further help on this.

To save the shape as PNG, you can utilize the following code snippet:
WriteableBitmap bmp = GetAsWritableBitmap();
using (var mediaLibrary = new MediaLibrary())
{
using (var stream = new MemoryStream())
{
var fileName = string.Format("Gs{0}.jpg", Guid.NewGuid());
bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
stream.Seek(0, SeekOrigin.Begin);
var picture = mediaLibrary.SavePicture(fileName, stream);
if (picture.Name.Contains(fileName)) return true;
}
}
Hope that helps.

Related

unable to create document border using itext 7

Using the code below I try to set a border around my document, but nothing shows up:
PdfWriter pdfWriter = new PdfWriter(toFile);
PdfDocument pdfDoc = new PdfDocument(pdfWriter); //Initialize PDF document
var pageSize = PageSize.LETTER;
var document = new iText.Layout.Document(pdfDoc, pageSize); // Initialize document
var fontTimesRoman = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.TIMES_ROMAN);
var fontTimesRomanBold = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.TIMES_BOLD);
var navy = new DeviceRgb(0, 0, 128);
var red = new DeviceRgb(139, 0, 0);
document.SetBorder(new SolidBorder(red, 18));
document.Add( new Paragraph(DateTime.Now.ToString("MMMM dd, yyyy"))
.SetFont(fontTimesRoman)
.SetTextAlignment(iText.Layout.Properties.TextAlignment.RIGHT)
//.SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.RIGHT)
.SetFontSize(11)
);
This won't work since the document is the size of the page and the Border will be outside of the page.
If you are trying to add a border to the edges of a Document you would need to do it in a PdfCanvas object
pdfDoc.AddNewPage();
var canvas = new PdfCanvas(pdfDoc, 1);
canvas.SetStokeColor(red);
canvas.SetLineWidth(18f);
canvas.Rectangle(0,1,pageSize.GetWidth()-1,pageSize.GetHeight()-1);
canvas.Stroke();
also don't forget to run
pdfDoc.Close();

C# Winforms how to resize image and save

hi I want to open a image file with openfiledialog and resize to upload to database so please help me with this code which I got from some where but I don't know how to make it working thanks
private void ResizeImg(double scaleFactor, Stream sourcePath, string tragetPath)
{
using (var image = System.Drawing.Image.FromStream(sourcePath))
{
var newWidth = (int)(image.Width * scaleFactor);
var newHeight = (int)(image.Height * scaleFactor);
var resizingImg = new Bitmap(newWidth, newHeight);
var resizeGraph = Graphics.FromImage(resizingImg);
resizeGraph.CompositingQuality = CompositingQuality.HighQuality;
resizeGraph.SmoothingMode = SmoothingMode.HighQuality;
resizeGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
resizeGraph.DrawImage(image, imageRectangle);
resizingImg.Save(targetPath, image.RawFormat);
}
}
Without specifying source and target rectangle, it doesn't stretch.
Change
resizeGraph.DrawImage(image, imageRectangle);
to
var srcRectangle = new Rectangle(0, 0, image.Width, image.Height);
resizeGraph.DrawImage(image, imageRectangle,srcRectangle, GraphicsUnit.Pixel);
BTW: Consider ImageFormat.Jpeg or .Png for Save, because you cannot write all image formats you can read.

Custom ShellTile

Is there a way to create a custom ShellTile that displays at start (after user pins our app)? I would like to resize and reposition a count number (label), but I can't find a way to do that.
You can't change the position or style of title or counter, but you can generate an image to display on the tile, for example:
WriteableBitmap bitmap = new WriteableBitmap(173, 173);
TextBlock textBlock = new TextBlock();
textBlock.TextWrapping = TextWrapping.Wrap;
textBlock.Foreground = new SolidColorBrush(Colors.White);
textBlock.FontSize = 22.0;
textBlock.Margin = new Thickness(12.0, 8.0, 8.0, 45.0);
textBlock.Text = "Lorem ipsum";
textBlock.HorizontalAlignment = HorizontalAlignment.Stretch;
textBlock.VerticalAlignment = VerticalAlignment.Stretch;
Grid layoutRoot = new Grid();
layoutRoot.Background = (Brush)App.Current.Resources["PhoneAccentBrush"];
layoutRoot.Width = 173.0;
layoutRoot.Height = 173.0;
layoutRoot.Children.Add(textBlock);
layoutRoot.Measure(new Size(173, 173));
layoutRoot.Arrange(new Rect(0, 0, 173, 173));
layoutRoot.UpdateLayout();
bitmap.Render(layoutRoot, null);
bitmap.Invalidate();
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
string fileName = "/Shared/ShellContent/BackgroundImage.jpg";
using (Stream fileStream = storage.CreateFile(fileName))
{
bitmap.SaveJpeg(fileStream, 173, 173, 0, 100);
}
}
StandardTileData tileData = new StandardTileData
{
BackgroundImage = new Uri("isostore:"/Shared/ShellContent/BackgroundImage.jpg, UriKind.Absolute),
Title = "Lorem Ipsum,
};
ShellTile.Create(new Uri("/MainPage.xaml", UriKind.Relative), tileData);

How to set WriteableBitmap back color for a live tile in Mango?

I'm trying to dynamically build a live tile.
It runs fine thanks to some SO suggestions and I have this code:
WriteableBitmap wbmp = new WriteableBitmap(173, 173);
TextBlock text = new TextBlock() { FontSize = (double)Resources["PhoneFontSizeLarge"], Foreground = new SolidColorBrush(Colors.White) };
text.Text = "my text";
wbmp.Render(text, new TranslateTransform() { Y = 20 });
wbmp.Invalidate();
// save image to isolated storage
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream imageStream = new IsolatedStorageFileStream("/Shared/ShellContent/MyImage.jpg", System.IO.FileMode.Create, isf))
{
wbmp.SaveJpeg(imageStream, wbmp.PixelWidth, wbmp.PixelHeight, 0, 100);
}
}
The problem is that the tile has a black (or, better, transparent) background. I would like to use accent background color, how can I do it?
Solved in this way:
Canvas can = new Canvas();
can.Background = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
can.Width = 173;
can.Height = 173;
wbmp.Render(can, null);
You're better to use Resources["TransparentBrush"] as the background, and then save to png, otherwise, your tile will be the wrong color on a theme change.

Image Rotation Animation in Windows Phone 7

Upon Taping an image, i want the image to rotate. I checked online but nothing seems helpful.
So how can this be done?
Any help would be appreciated.
UPDATE:
I found the solution and it appears to be pretty simple. The following code does the trick of rotation animation of an image:
Duration duration = new Duration(TimeSpan.FromSeconds(2));
Storyboard sb = new Storyboard();
sb.Duration = duration;
DoubleAnimation da = new DoubleAnimation();
da.Duration = duration;
sb.Children.Add(da);
RotateTransform rt = new RotateTransform();
Storyboard.SetTarget(da, rt);
Storyboard.SetTargetProperty(da, new PropertyPath("Angle"));
da.To = 360;
Search.RenderTransform = rt;
Search.RenderTransformOrigin = new Point(0.5, 0.5);
sb.Begin();
The following code does the trick rotate an image:
Duration duration = new Duration(TimeSpan.FromSeconds(2));
Storyboard sb = new Storyboard();
sb.Duration = duration;
DoubleAnimation da = new DoubleAnimation();
da.Duration = duration;
sb.Children.Add(da);
RotateTransform rt = new RotateTransform();
Storyboard.SetTarget(da, rt);
Storyboard.SetTargetProperty(da, new PropertyPath("Angle"));
da.To = 360;
Search.RenderTransform = rt;
Search.RenderTransformOrigin = new Point(0.5, 0.5);
sb.Begin();

Resources