【问题标题】:Save an Image with superimposed Label on the device在设备上保存带有叠加标签的图像
【发布时间】:2021-04-25 16:15:31
【问题描述】:

我的 ftp 网络空间中有一些图片,通过这种方法我可以在屏幕上查看它们,并在它们上方插入一个Label

var ImageUrl = "ftp://xxxxxxxxx.jpg";

//Download Image
byte[] ImgByte1 = WebClient.DownloadData(ImageUrl);
MemoryStream mStream1 = new MemoryStream(ImgByte1);

ObservableCollection<FraseClass> listFrasi = new ObservableCollection<FraseClass>
 {
    new FraseClass{Source=ImageSource.FromStream(() => mStream1)},
 }

XAML

<Image 
     Source="{Binding Source}"/>
<Label 
     Text="Hello"/>

我正在寻找一种能够将图像与叠加文本一起保存在设备上的方法。我试图搜索,但找不到任何东西来解决我的问题

【问题讨论】:

    标签: c# xaml xamarin.forms


    【解决方案1】:

    我正在寻找一种能够将图像与叠加文本一起保存在设备上的方法。我试图搜索,但找不到任何东西来解决我的问题

    根据您的描述,您想将从url下载的图像保存到本地存储中,对吗?

    如果是,我建议您可以使用Xamarin.Forms DependencyService 来执行此操作。

    首先,在共享代码中创建一个接口。

    public interface IImageFile
    {
        void SaveImage(string name, byte[] data, string location = "temp");
    }
    

    然后在Android平台上实现接口。不要忘记注册平台实现。

    [assembly: Dependency(typeof(ImageFile))]
    namespace FormsSample.Droid
    {
    public class ImageFile : IImageFile
    {
        public void SaveImage(string name, byte[] data, string location = "temp")
        {
            var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            documentsPath = System.IO.Path.Combine(documentsPath, "Orders", location);
            Directory.CreateDirectory(documentsPath);
    
            string filePath = System.IO.Path.Combine(documentsPath, name);
            using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
            {
                int length = data.Length;
                fs.Write(data, 0, length);
            }
        }
    
      
    }
    }
    

    最后,使用 DependencyService 解决平台实现

     private void btn1_Clicked(object sender, EventArgs e)
        {
    
            var ImageUrl = "ftp://xxxxxxxxx.jpg";
    
            //Download Image
            byte[] ImgByte1 = WebClient.DownloadData(ImageUrl);
            DependencyService.Get<IImageFile>().SaveImage("ImageName.jpg", ImgByte1, "imagesFolder");
        }
    

    您需要在AndroidMainfeast.xml 中添加权限WRITE_EXTERNAL_STORAGEREAD_EXTERNAL_STORAGE,然后您还需要在Android 6.0 中进行Runtime Permission Checks。在 Mainactivity.cs 中使用以下代码请求权限。

    private void checkpermission()
    {
        if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) == (int)Permission.Granted)
        {
            // We have permission, go ahead and use the writeexternalstorage.
        }
        else
        {
            // writeexternalstorage permission is not granted. If necessary display rationale & request.
        }
        if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) == (int)Permission.Granted)
        {
            // We have permission, go ahead and use the ReadExternalStorage.
        }
        else
        {
            // ReadExternalStorage permission is not granted. If necessary display rationale & request.
        }
    }
    

    IOS平台的详细信息,你可以看看:

    How to download image and save it in local storage using Xamarin-Forms.?

    更新:

    如果要在图片上添加文字水印,需要先将byte[]转换为位图添加文字。

     Android.Graphics.Bitmap mutableBitmap;
        Android.Graphics.Bitmap bitmap;
        public void ConvertImage(byte[] imageArray)
        {
             bitmap = BitmapFactory.DecodeByteArray(imageArray, 0, imageArray.Length);
           
        }
       
        public void Watermark()
        {            
            mutableBitmap = bitmap.Copy(Android.Graphics.Bitmap.Config.Argb8888, true);
            Canvas canvas = new Canvas(mutableBitmap);
            // Canvas canvas = new Canvas(bitmap);
            Paint paint = new Paint();
            paint.Color = Android.Graphics.Color.Black;
            paint.TextSize = 50;
            canvas.DrawText("hellod world", 50, 50, paint);
        }
    

    然后将位图转换为字节[],将图像保存到本地存储。

     public void convertbitmap(Bitmap bitmap)
        {
            bitmap = mutableBitmap;
            MemoryStream stream = new MemoryStream();
            bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
            byte[] bitmapData = stream.ToArray();
        }
    

    【讨论】:

    • 他为什么需要 DependencyService 来执行此操作?他可以直接从 NET Standard 项目写入文件。我相信他实际上是在询问将特定视图捕获为图像
    • @Jason 我认为操作想将从 url 下载的图像保存到 Xamarin.forms 中的本地设备存储中,也许我理解错了。
    • 根据您的建议,我可以将图像保存在设备上,这正是我所需要的,但我真正想做的就是保存一个在其中插入一些文本的图像。我已经用一个实际的例子更新了代码
    • @trecchino 你想在图片上添加文字水印,请看我的更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 1970-01-01
    相关资源
    最近更新 更多