I need to come up with a way to watermark user generated HTML and display it to the user.
I played around with modifying the HTML by setting a background image, but the solution didn't work if the any html element contained a background color or image.
So I thought it would be easier to display the HTML in a web browser control and watermark the control. Boy was I wrong.
I tried creating a user control and derive from WebBrowser and overwrite the paint method. That didn't work.
Next idea was to just draw on top of a webbrowser control.
here's what I've come up with. In this example I slapped a label, webbrowser control and two picture boxes on a form. I create the image and set it to both picture boxes. I put one picture box over the label and the other over the watermark.
The picturebox over the label displayed as I expected, but the picturebox over the webbrowser control did not.
I'm out of ideas. Any suggestions?
public partial class Form1 : Form
{
string WatermarkText = "Confidential";
Font WatermarkFont = new Font("Microsoft Sans Serif", 24, FontStyle.Bold);
int WatermarkAngle = 45;
Color WatermarkColor = Color.PaleTurquoise;
public Form1()
{
InitializeComponent();
AllowTransparency = true;
label1.AutoSize = false;
label1.Text = "Test Text";
label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
webBrowser1.Navigate("http://www.google.com");
var img = creategraphics(WatermarkText);
label1.Size = pictureBox2.Size = pictureBox1.Size = img.Size;
label1.Controls.Add(pictureBox1);
pictureBox1.Image = img;
pictureBox1.BackColor = Color.Transparent;
pictureBox1.Location = new Point(0, 0);
webBrowser1.Controls.Add(pictureBox2);
pictureBox2.Image = img;
pictureBox2.BackColor = Color.Transparent;
pictureBox2.Location = new Point(0, 0);
}
Image creategraphics(string text)
{
if (String.IsNullOrWhiteSpace(text)) return null;
var size = getWatermarkSize(text, WatermarkAngle, WatermarkFont, out double hypotenuse);
var img = new Bitmap(size.Width, size.Height);
var g = Graphics.FromImage(img);
var brush = new SolidBrush(WatermarkColor);
var stringFormat = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
g.RotateTransform((float)-WatermarkAngle);
g.DrawString(text, WatermarkFont, brush, new Point(0, (int)hypotenuse / 2), stringFormat);
return img;
}
private static Size getWatermarkSize(string text, int angle, Font font, out double hypotenuse)
{
if (angle > 90) throw new ArgumentOutOfRangeException("angle");
using (var bitmap = new Bitmap(1, 1))
using (var graphics = Graphics.FromImage(bitmap))
{
var size = graphics.MeasureString(text, font);
hypotenuse = size.Width + size.Height;
double radians = (angle / 180.0) * Math.PI;
var width = (Math.Cos(radians) * size.Width) + (Math.Sin(radians) * size.Height);
var height = (Math.Cos(radians) * size.Height) + (Math.Sin(radians) * size.Width);
return new Size((int)width, (int)height);
}
}
}
This is what I get
Related
I have a map using Xamarin.Forms.Googlemap in my xamarin forms app.
Is there a way to click on what is behind a pin?
I am creating polygons on the googlemap and then adding custom pins with orchard names on it.
Pin pin = new Pin()
{
Icon = CreateLabels(orchardname, 60),
Flat = true,
Address = string.Empty,
Label = string.Empty,
Position = Xamarin.Forms.GoogleMaps.Bounds.FromPositions(allpositions).Center,
Anchor = new Point(0.5, -1),
IsVisible = _showPropertyzoom
};
protected BitmapDescriptor CreateLabels(string Text, int size)
{
SKBitmap bitmap;
SKCanvas bitmapCanvas;
SKColor white = new SKColor(255,255,255);
SKColor blue = new SKColor(0,88,164);
SKPaint textPaintStroke = new SKPaint { TextSize = size, StrokeWidth=3, Color = white, FakeBoldText=true, Style = SKPaintStyle.Stroke};
SKPaint textPaint = new SKPaint { TextSize = size, Color = blue };
SKRect bounds = new SKRect();
textPaint.MeasureText(Text, ref bounds);
bitmap = new SKBitmap((int)(bounds.Right + 10), (int)(bounds.Height +5));
using (bitmapCanvas = new SKCanvas(bitmap))
{
bitmapCanvas.Clear();
bitmapCanvas.DrawText(Text, bounds.Left, -bounds.Top, textPaintStroke);
bitmapCanvas.DrawText(Text, bounds.Left, -bounds.Top, textPaint);
}
SKData data = SKImage.FromBitmap(bitmap).Encode(SKEncodedImageFormat.Png, 100);
var image = BitmapDescriptorFactory.FromStream(data.AsStream());
return image;
}
The problem i am having is when i click on a polygon to trigger the polygonclicked event i am clicking on the pin so nothing happens.
I can't just use the pinclicked event to trigger the polygonclicked event in this case, because the pin/label can span across multiple polygons.
I have a png image on top of any elements that are added to the working area. The user can click and drag new elements to the area and then print the picture they create. The png image is not moveable. The png image acts as a background image or like a container for the user's picture.
When I try to print it, the png image does not show. I get the elements to show nicely, but that is it. If I change a few things I am able to print only the png image and not the elements. How do I get them to print together?
//This puts my png image on top and unclickable
stage.addChild(seal);
seal.mouseEnabled = false;
//this brings the current element to the front
for (var fl_ChildIndex_5:int = 0;
fl_ChildIndex_5 < this.numChildren;
fl_ChildIndex_5++) {
this.getChildAt(fl_ChildIndex_5).addEventListener(MouseEvent.CLICK, fl_ClickToBringToFront_5);
}
function fl_ClickToBringToFront_5(event:MouseEvent):void {
this.addChild(event.currentTarget as DisplayObject);
}
//This is my print button
var button:Sprite = new Sprite();
button.graphics.beginFill(0xf67821, 1);
button.graphics.drawRect(0, 0, 120, 60);
button.graphics.endFill();
addChild(button);
button.addEventListener(MouseEvent.CLICK, printContent);
var myArea:MovieClip = root as MovieClip;
var myRectangle:Rectangle = new Rectangle(150,0,700,500);
function printContent(event:MouseEvent) {
var printJob:PrintJob = new PrintJob();
if (printJob.start()) {
if (myArea.width > printJob.pageWidth){
myArea.width = printJob.pageWidth;
myArea.scaleY = myArea.scaleX;
{printAsBitmap:true};
}
printJob.addPage(myArea, myRectangle);
}
printJob.send();
printJob = null;
}
NEWEST CODE
var workingarea:MovieClip = new MovieClip();
stage.addChild(workingarea);
var seal:Sprite = new Sprite();
seal = new SealImg();
seal.x = 475;
seal.y = 271;
workingarea.addChild(seal);
seal.mouseEnabled = false;
seal.mouseChildren = false;
I want to add border around TextBlock programatically in Windows Store Application.
How can I do this?
Thanks.
Create new project and add this code in OnNavigatedTo event.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var tb = new TextBlock()
{
Text = "...TextBlock with border...",
FontSize = 20
};
//To get actual height and width of TextBlock
tb.Arrange(new Rect(0, 0, Window.Current.Bounds.Width, Window.Current.Bounds.Height));
tb.Measure(new Size(Window.Current.Bounds.Width, Window.Current.Bounds.Height));
var border = new Border()
{
BorderThickness = new Thickness(3),
BorderBrush = new SolidColorBrush(Windows.UI.Colors.Red),
Height = tb.ActualHeight + 10,
Width = tb.ActualWidth + 10
};
var rootGrid = (Grid)(((Page)this).Content);
rootGrid.Children.Add(border);
border.Child = tb;
}
I want to be able to cahce HTML text as bitmap, for use in AlivePDF. My testing works well with TextArea or if the component is on the Stage or visible. But I want to be able to crunch text blocks that dont necessarily sit on screen. Does anyone know how to do this?
What I have failing so far
public static function rasterizeText( text:String, width:int = 100, height:int = 100, x:int = 0, y:int = 0 ):BitmapData {
var textRenderer:TextArea = new TextArea();
textRenderer.width = width;
textRenderer.height = height;
textRenderer.htmlText = text;
var bitdata:BitmapData = new BitmapData(width, height, true, 0xFF000000)
bitdata.draw( textRenderer );
return bitdata;
}
Your code should work fine. There is no need to add textRenderer to the stage in order to draw it into the BitmapData. You are drawing your textRenderer onto a solid black background. Maybe your text is also black and that is why you can not see it?
Try changing 0xFF000000 to e.g. 0xFFFF0000 and see if the text will show up.
No it wasn't a color issue. Also I had the color set to FF000000 which at least should have been showing up black. TextArea didn't seem to play nice with bitmaps, although TextField works perfectly fine.
Edit: I'm guessing TextArea wasn't working as spark or mx because they're a FlexSprite thingy, which act differently to normal AS3 Sprites. They'll defer rendering until they're added to the display list.
Working function:
public function rasterizeText( text:String,
width:int = 100, height:int = 100 ):BitmapData {
var tf:TextField = new TextField();
tf.multiline = true;
tf.wordWrap = true;
tf.width = width;
tf.height = height;
tf.htmlText = text;
var bd:BitmapData = new BitmapData(width, height, true,0x00000000);
bd.draw(tf);
return bd;
}
This was not working (I tried mx & spark textarea components):
public function rasterizeText( text:String,
width:int = 100, height:int = 100 ):BitmapData {
var textRenderer:TextArea = new TextArea();
textRenderer.width = width;
textRenderer.height = height;
textRenderer.textFlow = TextConverter.importToFlow( text, TextConverter.TEXT_FIELD_HTML_FORMAT );
var bitdata:BitmapData = new BitmapData( width, height, false, 0xFF000000 )
bitdata.draw( textRenderer );
return bitdata;
}
However it would work when drawing from the Stage:
public function rasterizeText( text:String,
width:int = 100, height:int = 100 ):BitmapData {
var textRenderer:TextArea = new TextArea();
textRenderer.width = width;
textRenderer.height = height;
textRenderer.textFlow = TextConverter.importToFlow( text, TextConverter.TEXT_FIELD_HTML_FORMAT );
canvas.addElement( textRenderer );
var bitdata:BitmapData = new BitmapData( width, height, false, 0xFF000000 )
bitdata.draw( canvas );
return bitdata;
}
I'm having a really weird problem with the MOUSE_OVER event. I'm building dynamic tabs representing mp3 songs containing textfields with info and a dynamic image for the cover art. I am trying to get a simple MOUSE_OVER working over the whole tab, such that you can select the next song to play.
I am using a Sprite with alpha 0 that overlays my whole tab (incl. the textFields) as a Listener for MOUSE_OVER and _OUT... I've checked by setting the alpha to something visible and it indeed covers my tab and follows it around as I move it (just making sure I'm not moving the tab without moving the hotspot). Also, I only create it once my cover art is loaded, ensuring that it will cover that too.
Now, when the tab is in the top position, everything is dandy. As soon as I move the tab to make space for the next tab, the textFields break my roll behaviour... just like that noob mistake of overlaying a sprite over the one that you're listening for MouseEvents on. But... the roll area is still on top of the field, I've set selectable and mouseEnabled to false on the textFields... nothing.
It is as if the mere fact of moving the whole tab now puts the textField on top of everything in my tab (whereas visually, it's still in its expected layer).
I'm using pixel fonts but tried it with system fonts, same thing... at my wits end here.
public function Tab(tune:Tune) {
_tune = tune;
mainSprite = new Sprite();
addChild(mainSprite);
drawBorder();
createFormat();
placeArtist();
placeTitle();
placeAlbum();
coverArt();
}
private function placeButton():void {
_button = new Sprite();
_button.graphics.beginFill(0xFF000,0);
_button.graphics.drawRect(0,0,229,40);
_button.graphics.endFill();
_button.addEventListener(MouseEvent.MOUSE_OVER, mouseListener);
_button.addEventListener(MouseEvent.MOUSE_OUT, mouseListener);
_button.buttonMode = true;
mainSprite.addChild(_button);
}
private function mouseListener(event:MouseEvent):void {
switch(event.type){
case MouseEvent.MOUSE_OVER :
hilite(true);
break;
case MouseEvent.MOUSE_OUT :
hilite(false);
break;
}
}
private function createFormat():void {
_format = new TextFormat();
_format.font = "FFF Neostandard";
_format.size = 8;
_format.color = 0xFFFFFF;
}
private function placeArtist():void {
var artist : TextField = new TextField();
artist.selectable = false;
artist.defaultTextFormat = _format;
artist.x = 41;
artist.y = 3;
artist.width = 135;
artist.text = _tune.artist;
artist.mouseEnabled = false;
mainSprite.addChild(artist);
}
private function placeTitle():void {
var title : TextField = new TextField();
title.selectable = false;
title.defaultTextFormat = _format;
title.x = 41;
title.y = 14;
title.width = 135;
title.text = _tune.title;
title.mouseEnabled = false;
mainSprite.addChild(title);
}
private function placeAlbum():void {
var album : TextField = new TextField();
album.selectable = false;
album.defaultTextFormat = _format;
album.x = 41;
album.y = 25;
album.width = 135;
album.text = _tune.album;
album.mouseEnabled = false;
mainSprite.addChild(album);
}
private function drawBorder():void {
_border = new Sprite();
_border.graphics.lineStyle(1, 0x545454);
_border.graphics.drawRect (0,0,229,40);
mainSprite.addChild(_border);
}
private function coverArt():void {
_image = new Sprite();
var imageLoader : Loader = new Loader();
_loaderInfo = imageLoader.contentLoaderInfo;
_loaderInfo.addEventListener(Event.COMPLETE, coverLoaded)
var image:URLRequest = new URLRequest(_tune.coverArt);
imageLoader.load(image);
_image.x = 1.5;
_image.y = 2;
_image.addChild(imageLoader);
}
private function coverLoaded(event:Event):void {
_loaderInfo.removeEventListener(Event.COMPLETE, coverLoaded);
var scaling : Number = IMAGE_SIZE / _image.width;
_image.scaleX = scaling;
_image.scaleY = scaling;
mainSprite.addChild (_image);
placeButton();
}
public function hilite(state:Boolean):void{
var col : ColorTransform = new ColorTransform();
if(state){
col.color = 0xFFFFFF;
} else {
col.color = 0x545454;
}
_border.transform.colorTransform = col;
}
Fixed it. What was happening was that I didn't set the height of the textfield. That was overshooting the tab and hence lying over the previously instantiated tab, blocking MouseOver... don't even ask.