xamarin android上传图片到服务器,Xamarin.Android 压缩图片并上传到WebServices

///

///图片转换成String流///

/// 文件名(不带file://)

///

public static string ImageToString(stringfile_path)

{//待上传图片路径//string uploadFile = file_path;//转化成文件//System.IO.FileInfo imgFile = new System.IO.FileInfo(uploadFile);

文件转化成字节

//byte[] imgByte = new byte[imgFile.Length];

//读文件

//System.IO.FileStream imgStream = imgFile.OpenRead();

//文件写入到字节数组

//imgStream.Read(imgByte, 0, Convert.ToInt32(imgFile.Length));

//字节数组转换成String类型

//string by = Convert.ToBase64String(imgByte);

上传到服务器 后面是文件名fileUp.UpdateFile(imgByte, Guid.NewGuid() + ".png");

//return imgByte;

Bitmap bitmap= BitmapFactory.DecodeFile(file_path); //将图片文件转换成bitmap 格式:/storage/emulated/0/DCIM/Camera/IMG_20180425_105725.jpg

string bitstring =BitmapToString(bitmap);

bitmap= null; //一定要清空,否则会导致OOM问题

GC.Collect();returnbitstring;

}///

///图片缩放处理///

/// Bitmap文件

/// 新图片宽度

/// 新图片高度

///

public static Bitmap zoomImage(Bitmap bgimage, double newWidth, doublenewHeight)

{//获取这个图片的宽和高

float width =bgimage.Width;float height =bgimage.Height;//创建操作图片用的matrix对象

Matrix matrix = newMatrix();//计算宽高缩放率

float scaleWidth = ((float)newWidth) /width;float scaleHeight = ((float)newHeight) /height;//缩放图片动作

matrix.PostScale(scaleWidth, scaleHeight);

Bitmap bitmap= Bitmap.CreateBitmap(bgimage, 0, 0, (int)width,

(int)height, matrix, true);returnbitmap;

}static stringBitmapToString(Bitmap bitmap)

{

Bitmap bit= zoomImage(bitmap, 750, 1000);//小图//质量压缩//MemoryStream stream = new MemoryStream();//bit.Compress(Bitmap.CompressFormat.Jpeg, 50, stream);//byte[] bitmapData = stream.ToArray();//Bitmap map = BitmapFactory.DecodeByteArray(bitmapData, 0, bitmapData.Length);//btn_imagetwo.SetImageBitmap(map);//Bitmap im = zoomImage(bitmap, 800, 900);//大图

MemoryStream big_stream = newMemoryStream();

bit.Compress(Bitmap.CompressFormat.Jpeg,80, big_stream);byte[] big_bitmapData =big_stream.ToArray();returnConvert.ToBase64String(big_bitmapData);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值