如何将.Png图像转换为.Jpeg图像格式而不保存到磁盘空间.. [英] How to convert .Png image into .Jpeg image format without save into disk space..

查看:94
本文介绍了如何将.Png图像转换为.Jpeg图像格式而不保存到磁盘空间..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这样的问题:我使用Image Thumbnail()方法将加载的图像转换为180X35等固定格式。

但是来到.png图像,我得到的黑色图像没有显示实际图像看。

如何重新解决这些问题?

我使用以下代码:



I got some issue like this: I convert loaded image into some fixed format like 180X35 using Image Thumbnail () method.
But coming to .png Image, I got black color image not shown the actual image looking.
How to reslove these issues?
I'm using the following code:

static void foo()
{
    if (ImgImages.UploadedFiles.Count > 0)
    {

        UploadedFile file = ImgImages.UploadedFiles[0];
        fileData = new byte[file.InputStream.Length];
        System.Drawing.Image myImage = System.Drawing.Image.FromStream(file.InputStream);
        // Compute thumbnail size.
        Size thumbnailSize = GetThumbnailSize(myImage);
        // Get thumbnail.
        System.Drawing.Image thumbnail = myImage.GetThumbnailImage(
            thumbnailSize.Width,
            thumbnailSize.Height,
            null, IntPtr.Zero
        );
        fileData = imageToByteArray(thumbnail);

        oCheck.Signature = fileData;

    }
}

static Size GetThumbnailSize(System.Drawing.Image original)
{
    // Maximum size of any dimension.
    const int MaxWidth = 180;
    const int Maxheight = 35;
    // Width and height.
    int originalWidth = original.Width;
    int originalHeight = original.Height;

    // Compute best factor to scale entire image based on larger dimension.
    double factor;
    double factor1;
    if (originalWidth > originalHeight)
    {
        factor = (double)MaxWidth / originalWidth;
        factor1 = (double)Maxheight / originalHeight;
    }
    else
    {
        factor = (double)MaxWidth / originalWidth;
        factor1 = (double)Maxheight / originalHeight;
    }

    // Return thumbnail size.
    //return new Size((int)(originalWidth * factor), (int)(originalHeight * factor));
    return new Size((int)(originalWidth * factor), (int)(originalHeight * factor1));
}

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
}





如何将.Png图像转换为.jpeg或.gif格式,而不使用image.save( c:\ imag.jpeg,.....)?



这里我不想将该图像保存到我的电脑中。 />
一旦将.Png图像加载到.Jpeg ...我想存储到byte []或将转换图像分配给某个图像对象。

我不想保存转换图像进入我的电脑空间。



How to convert .Png Image into .jpeg or .gif format,with out using image.save( "c:\imag.jpeg",.....)?

Here I don't want to "save" that image into My computer.
Once loaded .Png image into .Jpeg ...I want to store into byte[] or assign convert image to some image object.
I don't want save that converted image into "My computer " space.

推荐答案

if (ImgImages.UploadedFiles.Count > 0)
{
    UploadedFile file = ImgImages.UploadedFiles[0];
    fileData = new byte[file.InputStream.Length];
    System.Drawing.Image myImage = System.Drawing.Image.FromStream(
        file.InputStream
    );
    // Compute thumbnail size.
    Size thumbnailSize = GetThumbnailSize(myImage);
    // Get thumbnail.
    System.Drawing.Image thumbnail = myImage.GetThumbnailImage(
        thumbnailSize.Width,
        thumbnailSize.Height,
        null, IntPtr.Zero
    );
    fileData = imageToByteArray(thumbnail);
 
    oCheck.Signature = fileData;
}
 
static Size GetThumbnailSize(System.Drawing.Image original)
{
    // Maximum size of any dimension.
    const int MaxWidth = 180;
    const int Maxheight = 35;
    // Width and height.
    int originalWidth = original.Width;
    int originalHeight = original.Height;
 
    // Compute best factor to scale entire image based on larger dimension.
    double factor;
    double factor1;
    if (originalWidth > originalHeight)
    {
        factor = (double) MaxWidth / originalWidth;
        factor1 = (double)Maxheight / originalHeight;
    }
    else
    {
        factor = (double)MaxWidth / originalWidth;
        factor1 = (double)Maxheight / originalHeight;
    }

    // Return thumbnail size.
    //return new Size((int)(originalWidth * factor), (int)(originalHeight * factor));
    return new Size((int)(originalWidth * factor), (int)(originalHeight * factor1));
}
 
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
}


没有保存到磁盘,没有像.jpg或.png或任何图像文件格式。因此,您不需要在这方面进行任何转换。



但是您需要将所获得的数据转换为可以在内存中使用的内容。我强烈建议使用 System.Drawing.Image [ ^ ]类。在处理图像处理时,它远远优于字节数组。通过网络转移或真正快速的图像处理是另一回事。



因为图像是一个抽象类,你需要创建其派生类之一的实例。在你的情况下,谈论.jpg和.png,使用 System.Drawing之一。位图 [ ^ ]构造函数。



您的下一个测试是将图像分配给 System.Windows.Forms.PictureBox [ ^ ]检查是否可以看到它。当所有图像正确加载时,您可以进一步操作尺寸/颜色/任何内容。
Without saving to disk, there is nothing like .jpg or .png or whatever image file format. Therefore, you don't need to convert anything in this respect.

But you need to convert the data you get into something you can work with in memory. I strongly suggest to use the System.Drawing.Image[^] class for that. It's far superior to a byte array when having to deal with image manipulation. Transferring over a network or really fast image processing is another thing.

Since Image is an abstract class, you need to create instances of one of its derived classes. In your case, talking of .jpg and .png, use one of the System.Drawing.Bitmap[^] constructors.

Your next test is to assign the image to a System.Windows.Forms.PictureBox[^] to check if you can see it. When all images load correctly, you can step further and manipulate sizes/colours/whatever.


嗨sairam pamidi,



如果我理解你的问题问题是如何在更改格式的同时制作图像的内存副本?你可以这样做:



Hi sairam pamidi,

If I understand you right your Problem is just how to make an in-memory copy of your Image while changing the format? You could do something like this:

static Image ConvertImage(Image imageOriginal, ImageFormat formatTarget)
{
    Image imageConverted = null;

    using (MemoryStream ms = new MemoryStream())
    {
        imageOriginal.Save(ms, formatTarget);

        ms.Position = 0; // rewind stream

        imageConverted = Image.FromStream(ms);

    }

    return imageConverted;
}





并调用这样的函数:





and call the function like this:

Bitmap bmp = new Bitmap(@"C:\temp\test.png");
Bitmap bmpConverted = ConvertImage(bmp, ImageFormat.Gif) as Bitmap;


这篇关于如何将.Png图像转换为.Jpeg图像格式而不保存到磁盘空间..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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