需要使用带有C#的asp.net将所有图像类型转换为JPEG [英] Need to convert all image types to JPEG using asp.net with C#

查看:64
本文介绍了需要使用带有C#的asp.net将所有图像类型转换为JPEG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,







我目前的要求是我需要转换(* .tif ,* .png,*。位图和* .gif)图片类型在上传时需要使用带有C#技术的asp.net转换为JPEG类型..







任何人都可以分享相关事宜的代码或链接。





请帮忙...





提前致谢,

Subbu

Hi All,



I my current requirement i need to convert (*.tif,*.png,*.bitmap and *.gif)pictures type while uploading need to convert to JPEG type using the asp.net with C# technologies..



Can any body share the code or links for the subjected matter.


Kindly help on this..


Thanks in advance,
Subbu

推荐答案

您无法在上传过程中转换它们。您必须让用户上传图像文件,然后您可以尝试将其加载到Bitmap对象中并以新格式重新保存图像。



I'ved使用类似于此的代码来执行您之前描述的内容:

You cannot convert them during the upload. You have to let the user upload the image file, then you can try to load it into a Bitmap object and resave the image in a new format.

I'ved used code similar to this to do what you're describing before:
public static class ImageExtensions
{

    public static byte[] ToByteArray(this Bitmap sourceImage, ImageFormat format)
    {
        if (sourceImage == null)
            return null;

        using (MemoryStream targetStream = new MemoryStream())
        {
            sourceImage.Save(targetStream, format);
            targetStream.Close();

            return targetStream.ToArray();
        }
    }

    public static Bitmap ToBitmap(this byte[] bytes)
    {
        if (bytes == null)
            return (Bitmap)null;

        using (MemoryStream targetStream = new MemoryStream(bytes))
        {
            return new Bitmap(targetStream);
        }
    }

}



您可以将Bitmap转换为任意字节数组你想要的图像格式。然后将数组转换回Bitmap是一件简单的事情。


You can convert a Bitmap to an array of bytes in any image format you want. Then it's a simple matter to convert the array back to a Bitmap.


这篇关于需要使用带有C#的asp.net将所有图像类型转换为JPEG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆