Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从c中的字节数组中删除0值_Javascript_C#_Canvas - Fatal编程技术网

Javascript 从c中的字节数组中删除0值

Javascript 从c中的字节数组中删除0值,javascript,c#,canvas,Javascript,C#,Canvas,我有以下代码: [WebMethod] public static string SaveImg(string imgdata) { string Path = "UploadedImages/"; string folderPath = HttpContext.Current.Server.MapPath(Path); Guid gid=Guid.NewGuid(); string fileNameWitPath = folderPath + "img_" +

我有以下代码:

[WebMethod]
public static string SaveImg(string imgdata)
{

    string Path = "UploadedImages/";
    string folderPath = HttpContext.Current.Server.MapPath(Path);
    Guid gid=Guid.NewGuid();
    string fileNameWitPath = folderPath + "img_" +gid  + ".png";

    FileStream fs = new FileStream(fileNameWitPath, FileMode.Create);
    BinaryWriter bw = new BinaryWriter(fs);

    byte[] data = Convert.FromBase64String(imgdata);

    bw.Write(data);
    bw.Close();
    bw.Dispose();
    return  "/" + Path +"img_"+ gid + ".png";
}
witch是一个函数,它获取base64字符串并将其转换为二进制,然后将图像保存到服务器

base64数据取自画布,该画布在上载时加载图像 图像如下所示:

除非在新选项卡中打开示例图像,否则我的问题将不会显示,您可以看到它周围包含白色边框,因为当它上载到画布时,它不适合画布大小

我的问题是,在将0字节保存到服务器bw.writedata之前,如何从图像中删除这些白边框,从字节数组中删除0字节 我尝试了此代码,但不起作用:

int i = data.Length - 1;
while(data[i] == 0)
   --i;
// now data[i] is the last non-zero byte
byte[] bar = new byte[i+1];
Array.Copy(data, bar, i+1);
bw.Write(data);
这是客户端代码:

        var canvas = $("#imageCanvas")[0];
        var ctx = canvas.getContext("2d");
        var uploadInput = $('#UploadFile');
        var width = 382;
        var height = 295;
uploadInput.on('change', function (e) {
            RemoveDisabledClass();
            handleImage(e);
        });

        function handleImage(e) {
            var reader = new FileReader();
            reader.onload = function (event) {
                var img = new Image();
                img.onload = function () {
                    // var canvas = ctx.canvas;
                    var hRatio = canvas.width / img.width;
                    var vRatio = canvas.height / img.height;
                    var ratio = Math.min(hRatio, vRatio);
                    var centerShift_x = (canvas.width - img.width * ratio) / 2;
                    var centerShift_y = (canvas.height - img.height * ratio) / 2;
                    ctx.clearRect(0, 0, canvas.width, canvas.height);
                    ctx.drawImage(img, 0, 0, img.width, img.height,
                                       centerShift_x, centerShift_y, img.width * ratio, img.height * ratio);
                }
                img.src = event.target.result;

            }
            reader.readAsDataURL(e.target.files[0]);
        }

        $("#btn_save").click(function () {
            if ($("#btn_save").hasClass("disabled")) return;
            $("#dv_Error").css("display", "none");
            var image = document.getElementById("imageCanvas").toDataURL();
            image = image.replace('data:image/png;base64,', '');
            $.ajax({
                type: 'POST',
                url: 'Default.aspx/SaveImg',
                data: '{ "imgdata" : "' + image + '" }',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (data) {
                    window.location.href = data.d;

                },
                error: function (request, status, error) {
                    $("#dv_Error").css("display", "block");

                }

            }); //ENd Ajax
        }); //End Click event

如果您只想从字节数组中删除0,是否可以不使用LINQ?通过导入命名空间,您可以使用以下代码检查数组中的所有0值,并选择不是0的值,然后将其重写到数组中:

data = data.Where(x => x != 0).ToArray();
希望这对你有帮助

问候,,
Warren

如果您想简单地从字节数组中删除0,您可以不使用LINQ吗?通过导入命名空间,您可以使用以下代码检查数组中的所有0值,并选择不是0的值,然后将其重写到数组中:

data = data.Where(x => x != 0).ToArray();
希望这对你有帮助

问候,,
Warren

一种解决方案是使用类似ImageProcessor的东西-它包括一个能完全满足您需求的处理器:

   // Read a file and resize it.
   byte[] source = File.ReadAllBytes(file);

   using (MemoryStream inStream = new MemoryStream(source))
       using (MemoryStream outStream = new MemoryStream())
           using (ImageFactory imageFactory = new ImageFactory())
           {
               // Load, remove whitespace around image and save an image.
               imageFactory.Load(inStream)
                    .EntropyCrop()
                    .Save(outStream);

            } 
        }
    }
EntropyCrop处理器将图像裁剪到熵最大的区域。换句话说,它将删除图像周围的空白

ImageProcessor.web项目中还有一个基于url的web api

代码参考:


一种解决方案是使用类似ImageProcessor的东西—它包括一个能完全满足您需求的处理器:

   // Read a file and resize it.
   byte[] source = File.ReadAllBytes(file);

   using (MemoryStream inStream = new MemoryStream(source))
       using (MemoryStream outStream = new MemoryStream())
           using (ImageFactory imageFactory = new ImageFactory())
           {
               // Load, remove whitespace around image and save an image.
               imageFactory.Load(inStream)
                    .EntropyCrop()
                    .Save(outStream);

            } 
        }
    }
EntropyCrop处理器将图像裁剪到熵最大的区域。换句话说,它将删除图像周围的空白

ImageProcessor.web项目中还有一个基于url的web api

代码参考:


我认为试图修改字节[]来更改图像会使事情变得比实际需要困难得多。将图像放在画布上,然后将其导出为base64字符串的代码在哪里?更改该代码,使其仅保存包含实际图像数据的图像,而不是超大画布。请检查我的编辑我认为试图修改字节[]以更改图像会使事情变得更加困难。将图像放在画布上,然后将其导出为base64字符串的代码在哪里?更改代码,使其仅保存包含实际图像数据的图像,而不是超大画布。请检查我的编辑