C# 在设备上保存带有重叠标签的图像
我的ftp web空间中有一些图像,通过这种方法,我可以在屏幕上查看它们,并在它们上面插入一个C# 在设备上保存带有重叠标签的图像,c#,xaml,xamarin.forms,C#,Xaml,Xamarin.forms,我的ftp web空间中有一些图像,通过这种方法,我可以在屏幕上查看它们,并在它们上面插入一个标签 var ImageUrl = "ftp://xxxxxxxxx.jpg"; //Download Image byte[] ImgByte1 = WebClient.DownloadData(ImageUrl); MemoryStream mStream1 = new MemoryStream(ImgByte1); ObservableCollection<Frase
标签
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)},
}
var-ImageUrl=”ftp://xxxxxxxxx.jpg";
//下载图像
字节[]ImgByte1=WebClient.DownloadData(ImageUrl);
MemoryStream mStream1=新的MemoryStream(ImgByte1);
ObservableCollection listFrasi=新的ObservableCollection
{
新FraseClass{Source=ImageSource.FromStream(()=>mStream1)},
}
XAML
<Image
Source="{Binding Source}"/>
<Label
Text="Hello"/>
我正在寻找一种方法,能够保存的文字叠加在设备上的图像。我试图搜索,但找不到任何东西来解决我的问题
我正在寻找一种方法,能够保存的文字叠加在设备上的图像。我试图搜索,但找不到任何东西来解决我的问题
根据您的描述,您希望将从url下载的图像保存到本地存储中,对吗
如果是,我建议您可以使用
首先,在共享代码中创建一个接口
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");
}
有关IOS平台的详细信息,您可以查看:
更新:
若要在图像上添加文本水印,首先需要将字节[]转换为位图以添加文本
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标准项目中写入文件。我相信他实际上是在问,作为一个整体,如何捕捉一个特定的视图image@Jason我想op想要将从url下载的图像保存到Xamarin.forms中的本地设备存储中,也许我理解错了。按照您的建议,我可以在设备上保存图像,这正是我所需要的,但我想做的恰恰是保存一个图像,并在其中插入一些文本。我已经用一种实用的方法更新了代码example@trecchino如果要在图像上添加文本水印,请查看我的更新。
public void convertbitmap(Bitmap bitmap)
{
bitmap = mutableBitmap;
MemoryStream stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
byte[] bitmapData = stream.ToArray();
}