一个图片加在另一个图片之上

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_40029679/article/details/98970278

例子图片,

newBmp = new Bitmap(bmp1.Width, bmp1.Height+bmp2.Height);
g = Graphics.FromImage(newBmp)
g.DrawImage(bmp1, 0, 0);//下面的图片
g.DrawImage(bmp2, 0, bmp1.Height)//外部相框图片
save(newBmp)

///需要背景图片设定大小,刚好覆盖要的图片

        public Image ResizeFrontImg(int Width,int heigt)
    {
        Image imgToResize = staticClass.FrontImg;
        int sourceWidth = imgToResize.Width;
        //获取图片高度
        int sourceHeight = imgToResize.Height;

        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;
        //计算宽度的缩放比例
        nPercentW = ((float) Width / (float)sourceWidth);
        //计算高度的缩放比例
        nPercentH = ((float)heigt / (float)sourceHeight);

        if (nPercentH < nPercentW)
            nPercent = nPercentH;
        else
            nPercent = nPercentW;
        //期望的宽度
        int destWidth = (int)(sourceWidth * nPercent);
        //期望的高度
        int destHeight = (int)(sourceHeight * nPercent);

        Bitmap b = new Bitmap(destWidth, destHeight);
        Graphics g = Graphics.FromImage((System.Drawing.Image)b);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        //绘制图像
        g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
        g.Dispose();
        return (System.Drawing.Image)b;
   
    }

覆盖使用方法:

  /// <summary>
    /// 图片大小处理
    /// </summary>
    /// <param name="Inpath"></param>
    /// <param name="MaxLenth"></param>
    /// <param name="ZoomQuality"></param>
    /// <returns></returns>
    public Image PictrueZoomAndSaveFile(Image img, int MaxLenth, int ZoomQuality)
    { 
        Image ImageIn = new Bitmap(img);
        img.Dispose();
        ImageFormat tFormat = ImageIn.RawFormat;
        if (ImageIn != null)
        {
            double SizeZoom = 1;
            if (ImageIn.Height > MaxLenth || ImageIn.Width > MaxLenth)//比较大的图片处理
            {
                if (ImageIn.Width > ImageIn.Height)
                {
                    SizeZoom = MaxLenth / (double)ImageIn.Width;
                }
                else
                {
                    SizeZoom = MaxLenth / (double)ImageIn.Height;
                }
                int ImageHigeht = (int)Math.Ceiling(ImageIn.Height * SizeZoom);
                int ImageWidth = (int)Math.Ceiling(ImageIn.Width * SizeZoom);
                try
                {
                    Image data = null;
                    //用指定的大小和格式初始化Bitmap类的新实例 
                    using (Bitmap bitmap = new Bitmap(ImageWidth, ImageHigeht, PixelFormat.Format32bppArgb))
                    {
                        //从指定的Image对象创建新Graphics对象 
                        using (Graphics graphics = Graphics.FromImage(bitmap))
                        {
                            //清除整个绘图面并以透明背景色填充 
                            //graphics.Clear(Color.Transparent);
                            //在指定位置并且按指定大小绘制原图片对象 
                            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
                            graphics.DrawImage(ImageIn, new Rectangle(0, 0, ImageWidth, ImageHigeht));

                            if (staticClass.FrontImgIsUsed)//是否使用相框文件
                            {
                                Image FrontImage = ResizeFrontImg(ImageWidth, ImageHigeht);     //相框图片  
                                                                                                // ResizeFrontImg(img.Size);
                                graphics.DrawImage(FrontImage, new Rectangle(0, 0, ImageWidth, ImageHigeht));
                            } 
                            GC.Collect();
                        }
                        data = new Bitmap(bitmap);


                        //修改dpi
                        EncoderParameters ep = new EncoderParameters();
                        long[] qy = new long[1];
                        qy[0] = ZoomQuality;//设置压缩的比例1-100  
                        EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
                        ep.Param[0] = eParam;

                        ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                        ImageCodecInfo jpegICIinfo = null;
                        for (int x = 0; x < arrayICI.Length; x++)
                        {
                            if (arrayICI[x].FormatDescription.Equals("JPEG"))
                            {
                                jpegICIinfo = arrayICI[x];
                                break;
                            }
                        }
                        return data;

                    }

                }
                catch (Exception ex)
                {
                    return null;
                }
            }
            else//比较小的图片直接保存
            {
                return ImageIn;
            }

        }
        else
        {
            return null;
        }

    }
展开阅读全文

没有更多推荐了,返回首页