我在Xamarin.Forms 的一部分中使用 Xamarin 我需要將檔案(“image.png”)轉換為位圖,因為當專案運行時進入“中斷模式”并向我顯示此訊息“Java.Lang. IllegalArgumentException: '無法解碼影像。提供的影像必須是位圖。'"。所以我嘗試以多種方式轉換檔案,例如:
1_ 使用 System.Drawing.Bitmap 中的方法,但顯示此例外“此平臺不支持此操作”
2_ 無法使用 Android.Graphics,因為我在 xamarin.forms 中作業,而不是在 xamarin.android 中。
3_ 我嘗試將“image.png”轉換為 base64 或 byte[] 陣列,然后轉換為位圖,但問題與第一個問題相同,因為要從 byte[] 陣列或 base64 轉換為我使用過的位圖System.Drawing.Bitmap 中的方法。
4_ 我嘗試使用庫 SkiaSharp 但我沒有成功,因為我沒有找到太多關于如何將 .png 轉換為 SKBitmap 然后將 SKBitmap 轉換為 Bitmap 的資訊(即使我不知道這是否可能)。
5_最后我用應用程式將“image.png”轉換為“image.bmp”并在我的專案中使用檔案.bmp,但它也不起作用。
我必須將“image.png”保存在一個字串變數中,這就是想法。
如果你有 SkiaSharp 的解決方案,我會很高興
編輯
這是我的代碼的一部分,我只是將影像保存在變數中
Icon = "pin_blue.png";
//i can't use a path because in xamarin you have many size from the same
//image for the different size of the screen
編輯 2這是我在谷歌地圖中顯示圖釘的方法
private void ShowPins(List<PointsOfInterest> resultPointsOfInterest)
{
if (resultPointsOfInterest != null && resultPointsOfInterest.Any())
{
var location = Geolocation.GetLastKnownLocationAsync();
PointsOfInterest position = new PointsOfInterest();
if (location != null)
{
position.ccsm0166latitud = location.Result.Latitude;
position.ccsm0166longitud = location.Result.Longitude;
}
else {
position = resultPointsOfInterest.First();
}
//Distance = Distance.FromKilometers(new Random().Next(23,35));
Distance = Distance.FromKilometers(3);
Position = new Position(position.ccsm0166latitud, position.ccsm0166longitud);
PinsFiltered= Pins = new List<PinCustom>(resultPointsOfInterest.Select(
x => new PinCustom()
{
Position =
new Position(x.ccsm0166latitud, x.ccsm0166longitud),
Address = x.ccsm0166direccion,
Label = x.ccsm0166nombre,
Type = PinType.Place,
TypePointOfInterest = x.ccsm0166tipositio,
IconBM = Int32.Parse(x.ccsm0166tipositio) == (int)PointOfInterestType.branch ? PinCustom.ConvertToBitmap("pin_blue.png") :
Int32.Parse(x.ccsm0166tipositio) == (int)PointOfInterestType.branch ? "pin_blue.png" :
Int32.Parse(x.ccsm0166tipositio) == (int)PointOfInterestType.branchWithExtendedHours ? "pin_black.png" :
Int32.Parse(x.ccsm0166tipositio) == (int)PointOfInterestType.branchWithExtendedHours2 ? "pin_black.png" :
Int32.Parse(x.ccsm0166tipositio) == (int)PointOfInterestType.branchWithExtendedHours3 ? "pin_black.png" :
Int32.Parse(x.ccsm0166tipositio) == (int)PointOfInterestType.selfServiceTerminal ? "pin_green.png" :
Int32.Parse(x.ccsm0166tipositio) == (int)PointOfInterestType.atmServBox ? "pin_yellow.png" : string.Empty
}).ToList());
}
else
{
Pins = new List<PinCustom>();
}
}
這是我保存影像的類 Pin
public class PinCustom : Pin
{
public string TypePointOfInterest { get; set; }
public string Icon { get; set; }
public Bitmap { get; set; }
//Here i create this variable to save a bitmap but i don't know if i do the things well
}
這是我想在 googlemaps 中顯示的圖示 .png
固定藍色影像
uj5u.com熱心網友回復:
為此使用 ffmpeg 命令:ffmpeg -i image.png image.bmp
uj5u.com熱心網友回復:
要打開檔案并將其轉換為位元組陣列:
string directory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
// or your image directory, just replace it
var stream = File.OpenWrite(Path.Combine(directory, "image.png"));
byte[] buff = ConvertStreamToByteArray(stream);
stream.Close();
public static byte[] ConvertStreamToByteArray(Stream stream)
{
byte[] byteArray = new byte[16 * 1024];
using (MemoryStream mStream = new MemoryStream())
{
int bit;
while ((bit = stream.Read(byteArray, 0, byteArray.Length)) > 0)
{
mStream.Write(byteArray, 0, bit);
}
return mStream.ToArray();
}
}
然后,將此位元組陣列傳遞給 SKBitmap:
SKBitmap bmp = SKBitmap.Decode(buff);
編輯:
如果您想嘗試不使用ConvertStreamToByteArray():
byte[] buff = null;
stream.Write(buff, 0, buff.Length);
這Write將取決于您的stream. 在這個例子中,我使用FileStream.
編輯2:
string resourceID = "image.png";
Assembly assembly = GetType().GetTypeInfo().Assembly;
using (Stream stream = assembly.GetManifestResourceStream(resourceID))
{
resourceBitmap = SKBitmap.Decode(stream);
SKBitmap bmp = SKImage.FromBitmap(resourceBitmap);
}
然后你可以使用這個位圖來繪制和填充你的 SKCanvas:
canvas.DrawBitmap(bmp, 0, 0);
uj5u.com熱心網友回復:
當我以 .png 格式放置圖釘時,我有 5 種圖釘(圖釘是影像 .png)
如果您想自定義地圖中的圖釘,您可以簡單地使用自定義渲染器來實作這一點。
用于表示標記的圖示可以通過呼叫該MarkerOptions.SetIcon方法進行自定義。這可以通過重寫該CreateMarker方法來完成,該方法為添加到地圖的每個 Pin 呼叫:
protected override MarkerOptions CreateMarker(Pin pin)
{
var marker = new MarkerOptions();
marker.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
marker.SetTitle(pin.Label);
marker.SetSnippet(pin.Address);
marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.pin));
return marker;
}
如果要顯示不同的圖示,可以參考以下代碼:
protected override MarkerOptions CreateMarker(Pin pin)
{
var marker = new MarkerOptions();
marker.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
marker.SetTitle(pin.Label);
marker.SetSnippet(pin.Address);
var customPin = (Element as CustomMap).CustomPins.FirstOrDefault(p => p.Position == pin.Position);
if (customPin != null)
{
if (customPin.IconType == "corporate")
{
marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.corporate));
}
else if (customPin.IconType == "pickup")
{
marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.bus));
}
}
return marker;
}
有關更多資訊,請查看:自定義標記。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/424308.html
標籤:C# 视觉工作室 xamarin xamarin.forms 位图
