如何使用MonoTouch以编程方式将某些文本添加到静态图像? [英] how to add some text to a static image programatically, Using MonoTouch?

查看:138
本文介绍了如何使用MonoTouch以编程方式将某些文本添加到静态图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张尺寸为1024 * 768的静态图片,一边有一些徽标,

I have a static image of size 1024*768 with some logo on one side,

我希望在该图片上添加一些文字,例如:Page 1, (另一方面)

i want to have some text added to that image eg: Page 1, (on another side)

我从

public override void ViewDidLoad ()
    {
        try {
            base.ViewDidLoad ();
            UIImage ii = new UIImage (Path.Combine (NSBundle.MainBundle.BundleUrl.ToString ().Replace ("%20", " ").Replace ("file://", ""), "images2.png"));

            RectangleF wholeImageRect = new RectangleF (0, 0, ii.CGImage.Width, ii.CGImage.Height);
            imageView = new UIImageView (wholeImageRect);
            this.View.AddSubview (imageView);

            imageView.Image = DrawVerticalText ("Trail Text", 100, 100);
            Console.Write ("Switch to Simulator now to see ");
            Console.WriteLine ("some stupid graphics tricks");
        } catch (Exception ex) {

        }
    }

public static UIImage DrawVerticalText (string text, int width, int height)
    {
        try {
            float centerX = width / 2;
            float centerY = height / 2;

            //Create the graphics context
            byte[] mybyteArray;
            CGImage tt = null;
            UIImage ii = new UIImage (Path.Combine (NSBundle.MainBundle.BundleUrl.ToString ().Replace ("%20", " ").Replace ("file://", ""), "images2.png"));
            using (NSData imagedata = ii.AsPNG ()) {
                mybyteArray = new byte[imagedata.Length];
                System.Runtime.InteropServices.Marshal.Copy (imagedata.Bytes, mybyteArray, 0, Convert.ToInt32 (imagedata.Length));
                using (CGBitmapContext ctx = new CGBitmapContext (mybyteArray, width, height, 8, 4 * width, CGColorSpace.CreateDeviceRGB (), CGImageAlphaInfo.PremultipliedFirst)) {
                    //Set the font
                    ctx.SelectFont ("Arial", 16f, CGTextEncoding.MacRoman);

                    //Measure the text's width - This involves drawing an invisible string to calculate the X position difference
                    float start, end, textWidth;

                    //Get the texts current position
                    start = ctx.TextPosition.X; 
                    //Set the drawing mode to invisible
                    ctx.SetTextDrawingMode (CGTextDrawingMode.Invisible);
                    //Draw the text at the current position
                    ctx.ShowText (text);
                    //Get the end position
                    end = ctx.TextPosition.X;
                    //Subtract start from end to get the text's width
                    textWidth = end - start;

                    //Set the fill color to blue
                    ctx.SetRGBFillColor (0f, 0f, 1f, 1f);

                    //Set the drawing mode back to something that will actually draw Fill for example
                    ctx.SetTextDrawingMode (CGTextDrawingMode.Fill);

                    //Set the text rotation to 90 degrees - Vertical from bottom to top.
                    ctx.TextMatrix = CGAffineTransform.MakeRotation ((float)(360 * 0.01745329f));
                    //Draw the text at the center of the image.
                    ctx.ShowTextAtPoint (2, 2, text);
                    tt = ctx.ToImage ();
                }
            }
            //Return the image
            return UIImage.FromImage (tt);

        } catch (Exception ex) {
            return new UIImage (Path.Combine (NSBundle.MainBundle.BundleUrl.ToString ().Replace ("%20", " ").Replace ("file://", ""), "images2.png"));
        }
    }

我得到的输出如下

正如你所看到的,它在宽度方面完全拉伸,我需要这个解决任何建议???

As you can see it gets completely stretched in terms of width, i need this to be solved Any suggestions ???

同时原始图像上部没有任何内容,处理后显示多色图层,如何修复?

At the same time the original image has nothing in the upper part, where as after processing it shows multi coloured layer, how to fix that ??

推荐答案

为什么不直接在图像上绘制文字?也许你可以试试这个:

Why do you not draw your text directly to the image? Perhaps you can try this:

    private static UIImage PutTextOnImage(UIImage image, string text, float x, float y)
    {
        UIGraphics.BeginImageContext(new CGSize(image.Size.Width, image.Size.Height)); 
        using (CGContext context = UIGraphics.GetCurrentContext())
        {
            // Copy original image
            var rect = new CGRect(0, 0, image.Size.Width, image.Size.Height);
            context.SetFillColor(UIColor.Black.CGColor);
            image.Draw(rect);

            // Use ScaleCTM to correct upside-down imaging
            context.ScaleCTM(1f, -1f);

            // Set the fill color for the text
            context.SetTextDrawingMode(CGTextDrawingMode.Fill);
            context.SetFillColor(UIColor.FromRGB(255, 0, 0).CGColor);

            // Draw the text with textSize
            var textSize = 20f;
            context.SelectFont("Arial", textSize, CGTextEncoding.MacRoman);
            context.ShowTextAtPoint(x, y, text);
        }

        // Get the resulting image from context
        var resultImage = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();

        return resultImage;
    }

以上方法使用给定颜色和文本化在coords x,y处绘制文本。如果您想垂直使用它,则需要使用rotateCTM旋转文本。请记住,rotateCTM使用radius。

The above method draws your text at coords x, y with given color and textsize. If you want it vertically you need to rotate the text with rotateCTM. keep in mind rotateCTM uses radius.

将此添加到您使用的Context块(在DrawTextAtPoint之前):

Add this to your using Context block (before DrawTextAtPoint):

var angle = 90;    
var radius = 90 * (nfloat)Math.PI / 180;
context.RotateCTM(radius);

这篇关于如何使用MonoTouch以编程方式将某些文本添加到静态图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆