Graphics from windows forms to Silverlight? - c#

The following code works for windows forms.
How can I do this to Silverlight?
I guess there is no graphics object.
Bitmap bm = new Bitmap(600, 600);
Graphics g = Graphics.FromImage(bm);
Brush b = new LinearGradientBrush(
new Point(1, 1), new Point(600, 600),
Color.White, Color.Red);
Point[] points = new Point[]
{new Point(10, 10),
new Point(77, 500),
new Point(590, 100),
new Point(250, 590),
new Point(300, 410)};
g.FillPolygon(b, points);
bm.Save("testandoImagem.jpg", ImageFormat.Jpeg);

Alan, even from a SL client application you can do this all server side, you are not showing anything in the User Interface, I would put this code in the business layer which has full access to the .NET Framework. Keep in mind that from SL you can't save on the client machine directly so you should use another way.

You could render the shape as a WriteableBitmap, but Silverlight does not have built in support for encoding the data to JPEG.
See these StackOverflow questions:
How to save BitmapImage / WriteableBitmap using SaveFileDialog in Silverlight 3.0?
Best Jpeg Encoder for Silverlight 4.0
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap.render(v=VS.95).aspx
WriteableBitmap wb = new WriteableBitmap(600, 600);
Polygon p = new Polygon();
p.Points = new PointCollection() { new Point(10, 10), new Point(77, 500), new Point(590, 100), new Point(250, 590), new Point(300, 410) };
p.Fill = new LinearGradientBrush()
{
//Gradient angle is 0,0 to 1,1 by default
GradientStops = new GradientStopCollection() {
new GradientStop() { Color = Colors.White, Offset = 0 },
new GradientStop() { Color = Colors.Red, Offset = 1 } }
};
wb.Render(p, null);
wb.Invalidate();
//Save WriteableBitmap as described in other questions

Related

How to convet telerik radbarcode to bitmap without missing label?

I am using Telerik RadBarcode in my Asp.Net web application. See my code to generate the RadBarcode programmatically.
RadBarcode barcode = new RadBarcode();
barcode.Type = BarcodeType.Code128;
barcode.ID = "RadBarcode1";
rcode.Text = "656146114";
barcode.ShowText = true;
barcode.OutputType = BarcodeOutputType.SVG_VML;
Bitmap bitmap1 = new Bitmap(barcode.GetImage());
bitmap1.Save("D:\\" + "\\bmap.jpg");
While saving the barcode as an image the text under the barcode is missing in my case.
We can place a label below the barcode image to show the text and create one new bitmap image which contains the text and barcode data. Refer to the example below;
RadBarcode barcodeRB = new RadBarcode();
barcodeRB.Type = BarcodeType.Code128;
barcodeRB.Text ="123456789";
barcodeRB.ShowText = true;
barcodeRB.OutputType = BarcodeOutputType.SVG_VML;
Bitmap bitmap1 = new Bitmap(barcodeRB.GetImage());
System.Drawing.Image image = barcodeRB.GetImage();
Bitmap barCanvas = new Bitmap(435, 205);
barCanvas.SetResolution(90, 90);
using (Graphics gfx = Graphics.FromImage(barCanvas))
using (SolidBrush brush = new SolidBrush(Color.FromArgb(255, 255, 255)))
{
gfx.FillRectangle(brush, 0, 0, 435, 205);
}
using (Graphics gr = Graphics.FromImage(barCanvas))
{
RectangleF rectf = new RectangleF(150, 170, 300, 50);
gr.DrawImage(image, 20, 20, 400, 150);
gr.DrawString(studentTest.BarcodeString, new Font("GenericSansSerif", 26), Brushes.Black, rectf);
}
barCanvas.Save("D:\" + "\bmap.jpg");

How to Tile a Shape with Image and Texture Mask?

How i can apply a FillRectangle with texture Brush to source image via using Texture Mask?
Something like that... but how to use a texture mask?
Bitmap textile = new Bitmap("textile.png");
TextureBrush textileBrush = new TextureBrush(textile);
Bitmap outfit = new Bitmap("outfit.png");
Bitmap masksource = new Bitmap("mask.png");
Color bodyColorKey = Color.FromArgb(0, 255, 0);
//what i should to do next and how to apply FillRectangle?
using (Graphics g = Graphics.FromImage(outfit))
{
g.FillRectangle(textileBrush, new Rectangle(0, 0, ???, ???));
}
Outfit:
And his mask:
Textile texture:
And how it should looks after, on output:
Any ideas how it should looks for System.Drawing?
using the mask image, make transparent for the given colours and combine the images as follows.
Bitmap textile = new Bitmap("textile.png");
TextureBrush textileBrush = new TextureBrush(textile);
Bitmap outfit = new Bitmap("outfit.png");
Bitmap masksource = new Bitmap("mask.png");
Bitmap transparentImage = new Bitmap(outfit.Width, outfit.Height);
masksource.MakeTransparent(Color.FromArgb(255, 0, 255, 0));
using (Graphics g = Graphics.FromImage(transparentImage))
{
g.DrawImage(outfit, new Point(0, 0));
//g.DrawImage(textile, new Point(0, 0)); // use texture image
g.FillRectangle(textileBrush, g.ClipBounds); // use texture image as Tile mode
g.DrawImage(masksource, new Point(0, 0));
transparentImage.MakeTransparent(Color.FromArgb(255, 255, 0, 0));
transparentImage.MakeTransparent(Color.FromArgb(255, 0, 0, 255));
}
Bitmap outputImage = new Bitmap(outfit.Width, outfit.Height);
using (Graphics g = Graphics.FromImage(outputImage))
{
g.DrawImage(outfit, new Point(0, 0));
g.DrawImage(transparentImage, new Point(0, 0));
}
Only you need to fill the rectangle with texture image
//what i should to do next and how to apply FillRectangle?
using (Graphics g = Graphics.FromImage(outfit))
{
g.FillRectangle(textileBrush,g.ClipBounds);
}

Get form size of PARENT of current Panel

I'm running this code to create a BMP over my app when a dialog opens to reduce confusion for users on what cannot be accessed (until dialog is closed).
Bitmap bmp = new Bitmap(DisplayRectangle.Width, DisplayRectangle.Height);
using (Graphics G = Graphics.FromImage(bmp))
{
G.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
G.CopyFromScreen(this.PointToScreen(new Point(0, 0)), new Point(0, 0), this.DisplayRectangle.Size);
double percent = 0.60;
Color darken = Color.FromArgb((int)(255 * percent), Color.Black);
using (Brush brsh = new SolidBrush(darken))
{
G.FillRectangle(brsh, this.DisplayRectangle);
}
}
// put the darkened screenshot into a Panel and bring it to the front:
using (Panel p = new Panel())
{
p.Location = new Point(0, 0);
p.Size = this.ClientRectangle.Size;
p.BackgroundImage = bmp;
this.Controls.Add(p);
p.BringToFront();
// display your dialog somehow:
frmcollecting frmcollecting = new frmcollecting(pxid);
frmcollecting.ShowDialog();
The issue is that it creates the bmp over the Panel (pnlActive), however I want it over the whole app. How can I get the dimenions and change the DisplayRectangle to "frmMainPage"?

.Net 4.5 wpf printing issue, paper size is always NorthAmericaLetter

I have an issue with printing in WPF. In .Net 4.5 it always prints with paper size NorthAmericaLetter. In .Net Framework 3.5 it prints on the correct paper size, which I defined in Code. I tried it in the same Solution, with the same Code and just changed the Target Framework.
My question is, am I missing something in the PrintTicket configuration or how can I set the paper size in .Net 4.5 correctly?
var visual = new DrawingVisual();
using (var context = visual.RenderOpen())
{
context.DrawRectangle(Brushes.CadetBlue, new Pen(Brushes.Black, 2),
new Rect(new Point(0, 0), new Size(793, 1122)));
context.DrawRectangle(Brushes.Brown, new Pen(Brushes.Black, 2),
new Rect(new Point(20, 20), new Size(40, 40)));
context.DrawRectangle(Brushes.Brown, new Pen(Brushes.Black, 2),
new Rect(new Point(20, 257), new Size(40, 40)));
}
var queue = new LocalPrintServer().DefaultPrintQueue;
queue.UserPrintTicket.PageMediaSize = queue
.GetPrintCapabilities()
.PageMediaSizeCapability
.Single(x => x.PageMediaSizeName == PageMediaSizeName.ISOA4);
queue.UserPrintTicket.PageOrientation = PageOrientation.Portrait;
var writer = PrintQueue.CreateXpsDocumentWriter(queue);
writer.Write(visual);
PageMediaSize pageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA4);
if (printCapabilities.PageMediaSizeCapability.Contains(pageMediaSize))
{
PrintTicket deltaPrintTicket = new PrintTicket {PageMediaSize = pageMediaSize };
var result =
queue.MergeAndValidatePrintTicket(queue.UserPrintTicket,
deltaPrintTicket);
if (result.ValidatedPrintTicket.PageMediaSize == pageMediaSize)
{
queue.UserPrintTicket = result.ValidatedPrintTicket;
queue.Commit();
}
}
You'll have to merge and validate the printticket:
Maybe queue.Commit(); is missing?

plotting points on a form

I want to plot points over an image that are a bit transparent. As in I'm able to see over what area are they present. Is there any way on C# .net platform to do so.??
Thanks.
This is one way to do it.
Image bitmap = new Bitmap(100, 100); // sample image, load your real image from file here
using (var g = Graphics.FromImage(bitmap))
{
g.FillRectangle(Brushes.Red, new Rectangle(0, 0, bitmap.Width, bitmap.Height)); // Just to fill the background on the sample image, remove this
var transparentColor = Color.FromArgb(127, Color.Blue); // Create a semitransparent color
using(Brush brush = new SolidBrush(transparentColor))
{
// Create the dot
g.FillEllipse(brush, new Rectangle(10, 10, 25, 25));
// Create another dot
g.FillEllipse(brush, new Rectangle(25, 15, 25, 25));
}
}
myPictureBox.Image = bitmap; // display the image in an Imagebox (optional, you might use your image somewhere else)

Categories