如何压缩为JPEG2000格式的图像 [英] How to compress to image in the form of JPEG2000
问题描述
大家好,
我想以JPEG2000的形式压缩图像
使用宽高比,最大尺寸为150(高度,宽度),
我想将图片缩小到1kb(最大1100个字节)
我以以下方式编写,在这种情况下,我可以正确获得宽高比,但是
我将图片大小设为4至5 kb
1)如何将此大小减小到1kb(最大1100个字节).
2)我必须使用最大尺寸为150(高度,宽度)的
请向我发送这两个问题的代码.
我的代码是:
Hi all,
I want to compress the images in the form of JPEG2000
and maximum size is 150(height, width) by using aspect ratio,
and I want to reduce the image to 1kb (maximum 1100 bytes)
I have written in the following way , In this case I got aspect ration properly but
I got image size to 4 to 5 kb
1)How to reduce this size to 1kb (maximum 1100 bytes) .
2)I must use maximum size is 150(height, width)
Please send me code for these two questions.
My code is :
public void Resize(Image originalImage)
{
int targetHeight, targetWidth,targetsize=150;
if(originalImage.Height>originalImage.Width)
{
targetHeight = targetSize;
targetWidth = (int)(originalImage.Width * ((float)targetSize / (float)originalImage.Height));
}
else
{
targetWidth = targetSize;
targetHeight = (int)(originalImage.Height * ((float)targetSize / (float)originalImage.Width));
}
Bitmap newImage = new Bitmap(originalImage, targetWidth, targetHeight);
Graphics g = Graphics.FromImage(newImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
originalImage.Dispose();
newImage.Save(@"D:\Images\hello.j2k", System.Drawing.Imaging.ImageFormat.Jpeg);
}
3)如何以字节格式上传此压缩图像?
请向我发送以上三个问题的答案
谢谢
[edit]已添加代码块-OriginalGriff [/edit]
3)How to upload this compressed image in byte format ?
Please send me answers for above three questions
Thank you
[edit]Code Block added - OriginalGriff[/edit]
推荐答案
1)对于Jpeg文件,无法保证图像文件具有任何特定大小:取决于图像数据可以应用多少压缩.所以,您的第一个问题没有答案...
2)您已提供问题2的代码:问题出在哪里?
3)上传到哪里?你想做什么?
顺便说一句:您没有创建originalImage,所以您不应废弃它:如果在此方法之外使用它会怎样.仅处置您创建的项目.不过,您绝对应该处置图形"g"和位图"newImage".
1) With a Jpeg file, there is no way to guarantee that the image file is any particular size: it depends on the image data how much compression can be applied. So, your first question has no answer...
2) You have provided code for question 2: what is the problem?
3) Upload to where? What are you trying to do?
BTW: You did not create originalImage, so you should not be disposing of it: what if it is used outside this method. Only dispose items you created. You should definitely Dispose your Graphics "g" and Bitmap "newImage" though.
这篇关于如何压缩为JPEG2000格式的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!