首页 新闻 会员 周边 捐助

如何把后台生成的图片传给前端,求详细代码!新手!

0
悬赏园豆:10 [已解决问题] 解决于 2018-05-21 17:27

如何把后台生成的图片传给前端,求详细代码!新手!

问题补充:

java后台操作

tuwosh的主页 tuwosh | 初学一级 | 园豆:18
提问于:2017-06-29 11:52
< >
分享
最佳答案
0

对图片的操作:

internal static byte[] GetImageBinaryFromPath(string imageFullPath)

        {

            byte[] imageByte;

            try

            {

                #region FromWeb

                //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imageSource);

                //HttpWebResponse response = (HttpWebResponse)request.GetResponse();

 

                //if (request.HaveResponse)

                //{

                //    if (response.StatusCode == System.Net.HttpStatusCode.OK)

                //    {

                //        Stream receiveStream = response.GetResponseStream();

                //        byte[] buffer = new byte[16 * 1024];

                //        using (MemoryStream ms = new MemoryStream())

                //        {

                //            int read;

                //            while (receiveStream != null && (read = receiveStream.Read(buffer, 0, buffer.Length)) > 0)

                //            {

                //                ms.Write(buffer, 0, read);

                //            }

                //            return ms.ToArray();

                //        }

                //    }

                //}

                #endregion

                Image img = Image.FromFile(imageFullPath);

                using (MemoryStream ms = new MemoryStream())

                {

                    if (imageFullPath.ToLower().Contains(".jpg"))

                    {

                        img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

                    }

                    else if (imageFullPath.ToLower().Contains(".png"))

                    {

                        img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

                    }

                    else

                        throw new Exception("Image Extension not match!");

                    return imageByte = ms.ToArray();

                }

            }

            catch (Exception)

            { return null; }

 

        }

 

 

 

internal static string CreateBase64Image(byte[] fileBytes, int width = 0, int height = 0)

        {

            Image streamImage;

            /* Ensure we've streamed the document out correctly before we commit to the conversion */

            using (MemoryStream ms = new MemoryStream(fileBytes))

            {

                streamImage = Image.FromStream(ms);

                if (width > 0 && height > 0)

                {

                    streamImage = ResizeImage(streamImage, width, height);

                }

                var memStream = new MemoryStream();

                streamImage.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg);

                return Convert.ToBase64String(memStream.ToArray());

            }

        }

 

        internal static Image ResizeImage(Image image, int width, int height)

        {

            Bitmap new_image = new Bitmap(width, height);

            Graphics g = Graphics.FromImage((Image)new_image);

            g.InterpolationMode = InterpolationMode.High;

            g.DrawImage(image, 0, 0, width, height);

            return new_image;

        }

收获园豆:10
肖恩部落 | 小虾三级 |园豆:585 | 2017-06-30 12:00
其他回答(4)
0

后台生成图片,保存到临时文件夹,返回图片的路径给前台即可。和上传图片的道理一样,先保存文件,后返回!

大楚打码人 | 园豆:4313 (老鸟四级) | 2017-06-29 11:56

具体代码有吗

支持(0) 反对(0) tuwosh | 园豆:18 (初学一级) | 2017-06-29 11:59
0

我有个验证码api是把图片转为字符串传递给前端使用的。

参考下:把图片转为base64字符串传递给前端,或者你搜搜别的方法

http://www.cnblogs.com/feiyun126/archive/2013/02/04/2892052.html

开山怪不怪 | 园豆:544 (小虾三级) | 2017-06-29 13:50
0

不需要保存成文件,这是浪费IO,直接内存创建Image,然后输出到流(或者也可以拿到writer,把内存流的write到此处也可以,反正效果差不多,都是内存流到网络流),最后记得把http头改一下就行了,即使你是传给客户端js解析(比如需要进行base64赋值),建议也不要在服务端做(浪费多),js是可以把此内容转换成该编码的。

eg1.

Page.render(writer)

{

  create image;

  image=>stream;

  write(stream);

  //if image show
    =>change doc.mimetype;

}

花飘水流兮 | 园豆:13615 (专家六级) | 2017-06-30 00:32
0

提供2种方法:

1.后端可以生成一个图片地址,然后返回给前端
2.返回base64

ycyzharry | 园豆:25683 (高人七级) | 2017-06-30 10:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册