System.Drawing.Image 怎么调整Image的宽高?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了System.Drawing.Image 怎么调整Image的宽高?相关的知识,希望对你有一定的参考价值。

调整办法是在这段代码中,创建了一个新的实例并且设置尺寸:
// SET THE IMAGEVIEW DIMENSIONS
int dimens = 120;
float density = activity.getResources().getDisplayMetrics().density;
int finalDimens = (int)(dimens * density);
LinearLayout.LayoutParams imgvwDimens =
new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgAlbumPhoto.setLayoutParams(imgvwDimens);
// SET SCALETYPE
imgAlbumPhoto.setScaleType(ScaleType.CENTER_CROP);
// SET THE MARGIN
int dimensMargin = 5;
float densityMargin = activity.getResources().getDisplayMetrics().density;
int finalDimensMargin = (int)(dimensMargin * densityMargin);
LinearLayout.LayoutParams imgvwMargin =
new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgvwMargin.setMargins
(finalDimensMargin, finalDimensMargin, finalDimensMargin, finalDim
ensMargin);

改变已存在的ImageView尺寸,使用这个:
coupon_img.setLayoutParams(new LayoutParams(100, 100));
参考技术A 当图片上传成功后,通过System.Drawing命名空间中的类对图片进行加工。首先创建图片的Image对象,然后对Image对象创建一个Graphics对象,通过Graphics对象可以在图片上添加文件(DrawString),然后保存图像就行了,大致应该是
float x, y, width, height;
System.Drawing.Font f = new System.Drawing.Font("宋体", 8.0);
System.Drawing.SolidBrush b = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
System.Drawing.Image img = System.Drawing.Image.FromFile("filename.ext");
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
g.DrawString("xxxx", f, b, new System.Drawing.RectangleF(x, y, width, height));
img.Save("filename_watermark.ext");

你还可以用System.Drawing.Graphics.MeasureString来测量你要绘制的字符串的尺寸,然后结合Image的Width和Height属性,动态计算上面的x,y,width,height的值,以便可以把字符串绘制在合适的地方。

如何更改无法从 'System.Drawing.Image' 转换为 'System.Drawing.Image[]?

【中文标题】如何更改无法从 \'System.Drawing.Image\' 转换为 \'System.Drawing.Image[]?【英文标题】:How to change cannot convert from 'System.Drawing.Image' to 'System.Drawing.Image[]?如何更改无法从 'System.Drawing.Image' 转换为 'System.Drawing.Image[]? 【发布时间】:2022-01-24 01:27:47 【问题描述】:

大家好,我正在尝试将 'System.Drawing.Image' 更改为 'System.Drawing.Image[] 以在我的代码中使用,因此如何使它工作,因为我需要合并 tiff 图像以存在 Multi Tif 图像谢谢。 如果我需要在选择页面后添加页面以存在多 tif 图像怎么办?如果还有其他方法可以帮助我吗?

    Image img2 = Image.FromFile(imagename1);
    Image img = Image.FromFile(imagename2);
    Tiff.TiffUtil.saveImageExistingMultiplePage(img2, img, 2, 
    imagename2);



public static void saveImageExistingMultiplePage(Image[] bmp, Image origionalFile, int PageNumber, string location)
            
                try
                
                    //Now load the Codecs
                    ImageCodecInfo codecInfo = getCodec();

                    System.Drawing.Imaging.Encoder saveEncoder;
                    System.Drawing.Imaging.Encoder compressionEncoder;
                    EncoderParameter saveEncodeParam;
                    EncoderParameter compressionEncodeParam;
                    EncoderParameters encoderParams = new EncoderParameters(2);
                    Bitmap pages;
                    Bitmap nextPage;

                    saveEncoder = System.Drawing.Imaging.Encoder.SaveFlag;
                    compressionEncoder = System.Drawing.Imaging.Encoder.Compression;

                    origionalFile.SelectActiveFrame(FrameDimension.Page, 0);
                    pages = new Bitmap(origionalFile);
                    pages = ConvertToBitonal(pages);

                    // Save the first page (frame).
                    saveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.MultiFrame);
                    compressionEncodeParam = new EncoderParameter(compressionEncoder, (long)EncoderValue.CompressionCCITT4);
                    encoderParams.Param[0] = compressionEncodeParam;
                    encoderParams.Param[1] = saveEncodeParam;

                    pages.Save(location, codecInfo, encoderParams);

                    for (int i = 1; i < PageNumber; i++)
                    
                        saveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.FrameDimensionPage);
                        compressionEncodeParam = new EncoderParameter(compressionEncoder, (long)EncoderValue.CompressionCCITT4);
                        encoderParams.Param[0] = compressionEncodeParam;
                        encoderParams.Param[1] = saveEncodeParam;

                        origionalFile.SelectActiveFrame(FrameDimension.Page, i);
                        nextPage = new Bitmap(origionalFile);
                        nextPage = ConvertToBitonal(nextPage);
                        pages.SaveAdd(nextPage, encoderParams);
                    

                    for (int i = 0; i < bmp.Length; i++)
                    
                        saveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.FrameDimensionPage);
                        compressionEncodeParam = new EncoderParameter(compressionEncoder, (long)EncoderValue.CompressionCCITT4);
                        encoderParams.Param[0] = compressionEncodeParam;
                        encoderParams.Param[1] = saveEncodeParam;
                        bmp[i] = (Bitmap)ConvertToBitonal((Bitmap)bmp[i]);
                        pages.SaveAdd(bmp[i], encoderParams);
                    

                    saveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.Flush);
                    encoderParams.Param[0] = saveEncodeParam;
                    pages.SaveAdd(encoderParams);
                
                catch (System.Exception ee)
                
                    throw ee;
                
            

【问题讨论】:

【参考方案1】:
// initialize an image array and add your image to it
Image[] img2 = new Image[]  Image.FromFile(imagename1) ;

// the rest of your code stays the same but 'img2' is now an array of images
// so the call to 'saveImageExistingMultiplePage' will accept it.
// ...

Tiff.TiffUtil.saveImageExistingMultiplePage(img2, img, 2, imagename2);

// ... continue your code

【讨论】:

以上是关于System.Drawing.Image 怎么调整Image的宽高?的主要内容,如果未能解决你的问题,请参考以下文章

如何更改无法从 'System.Drawing.Image' 转换为 'System.Drawing.Image[]?

将 DataTemplate 中的图像绑定到 System.Drawing.Image

如何将图像 url 转换为 system.drawing.image

System.Drawing.Image.FromFile() 上的内存不足异常

System.Drawing.GDIPlus.CheckStatus System.Drawing.Image.FromFile System.ArgumentException [GDI + sta

参数 5:无法从 'System.Drawing.Image' 转换为 'string' - 从主类调用 class1