c# - UIImage PNG着色

标签 c# ios xamarin uiimage

我想在用户点击 PNG 时为其着色。

我在 Xcode 中找到了一个例子 here但是当我在 C# 中尝试时,我无法检索图像。

这是我写的代码:

public UIImage DrawSelectedBorder(UIImage image)
{
    UIGraphics.BeginImageContextWithOptions(image.Size, false, image.CurrentScale); // for correct resolution on retina, thanks @MobileVet
    CGContext context = UIGraphics.GetCurrentContext();

    context.TranslateCTM(0, image.Size.Height);
    context.ScaleCTM((System.nfloat)1.0, (System.nfloat)(-1.0));

    CGRect rect = new CGRect(0, 0, image.Size.Width, image.Size.Height);

    context.SetBlendMode(CGBlendMode.Normal);
    context.SetFillColor(UIColor.Black.CGColor);
    context.FillRect(rect);


    // draw original image
    context.SetBlendMode(CGBlendMode.Normal);
    context.DrawImage(rect,image.CGImage);

    // tint image (losing alpha) - the luminosity of the original image is preserved
    context.SetBlendMode(CGBlendMode.Color);
    context.FillRect(rect);

    // mask by alpha values of original image
    context.SetBlendMode(CGBlendMode.DestinationIn);
    context.DrawImage(rect, image.CGImage);

    UIImage coloredImage = UIGraphics.GetImageFromCurrentImageContext();
    UIGraphics.EndImageContext();

    return coloredImage;
}

最佳答案

刚试过你的代码。一切正常,但您没有选择要填充到 PNG 中的颜色。

改变:

// tint image (losing alpha) - the luminosity of the original image is preserved
context.SetBlendMode(CGBlendMode.Color);
context.FillRect(rect);

到:

// tint image (losing alpha) - the luminosity of the original image is preserved
context.SetBlendMode(CGBlendMode.Color);
context.SetFillColor(UIColor.Red.CGColor);
context.FillRect(rect);

关于c# - UIImage PNG着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43408862/

相关文章:

c# - 如何使用 Windows 登录凭据使用 C# 进行代理身份验证

c# - 如何以编程方式最小化打开的窗口文件夹

c# - 在 Xamarin iOS 应用程序中使用 UIApplicationDelegate.DidReceiveRemoteNotification 处理远程通知

ios - 在 Playground 的 UIViewController 上自动调整 UILabel 和 UIView 的大小

ios - CoreTelephony 崩溃(不可重现)

c# - 从日期时间解析额外的字符

ios - NSLineBreakMode ByWordWrapping 在最新更新后不起作用

android - Xamarin Android 与 MvvmCross 冲突

c# - 动态更改 TextView 的文本

c# - 任务完成后永远不会更新属性