当前位置: 首页>>代码示例>>C#>>正文


C# Pixbuf.Scale方法代码示例

本文整理汇总了C#中Gdk.Pixbuf.Scale方法的典型用法代码示例。如果您正苦于以下问题:C# Pixbuf.Scale方法的具体用法?C# Pixbuf.Scale怎么用?C# Pixbuf.Scale使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Gdk.Pixbuf的用法示例。


在下文中一共展示了Pixbuf.Scale方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: LoadAndScaleImage

        //
        // Loads the image from disk and scales it to certain size
        // It is used to generate the final images and thumbnails
        //
        void LoadAndScaleImage(int width, int height)
        {
            if (image == null)
                throw new InvalidOperationException ("SlideImage.LoadAndScaleImage: no filename defined for image");

            if (width <= 0 || height <= 0)
                throw new InvalidOperationException ("SlideImage.LoadAndScaleImage: width and height should be > 0");

            Logger.Debug ("SlideImage.LoadAndScaleImage. {0} {1} {2}", image, width, height);

            int max_w = width;    // max target width
            int max_h = height;   // max target height
            double target_ratio = (double) max_w / (double) max_h;  // aspect ratio target
            double scale, original_ratio, corrected_ratio;
            Gdk.Pixbuf raw_image, processed_image;

            raw_image = new Gdk.Pixbuf (image);

            Logger.Debug ("SlideImage.LoadAndScaleImage. Load image w:{0} h:{1} channels:{2}", raw_image.Width, raw_image.Height, raw_image.NChannels);

            if (raw_image.NChannels != 3 && raw_image.NChannels != 4) {
                Logger.Error ("SlideImage.LoadAndScaleImage. Image {0} with unsupported number of channels ({1})", image, raw_image.NChannels);
                return;
            }

            processed_image = new Gdk.Pixbuf (Colorspace.Rgb, raw_image.NChannels == 3 ? false : true, 8, max_w, max_h);
            processed_image.Fill (0x00000000);

            original_ratio = (double) raw_image.Width / (double) raw_image.Height;

            // Image is larger that target resolution, we need to rescale
            if (raw_image.Width > max_w || raw_image.Height > max_h) {
                if (original_ratio < 1) { // If X is properly scaled (the smaller), Y will be too
                    if (original_ratio > target_ratio)
                        corrected_ratio = target_ratio / original_ratio;
                    else
                        corrected_ratio = original_ratio / target_ratio;

                    scale = (double) max_w / (double) raw_image.Width;
                    h = (int) ((double) raw_image.Width * scale / original_ratio * corrected_ratio);
                    w = (int) ((double) raw_image.Width * scale * corrected_ratio);
                } else {  // If Y is properly scaled (the smaller), X will be too (used path for NTSC and PAL resolutions)
                    if (original_ratio > target_ratio)
                        corrected_ratio = target_ratio / original_ratio;
                    else
                        corrected_ratio = original_ratio / target_ratio;

                    scale = (double) max_h / (double) raw_image.Height;
                    h = (int) ((double) raw_image.Width * scale / original_ratio * corrected_ratio);
                    w = (int) ((double) raw_image.Width * scale * corrected_ratio);
                }
            } else { // No need to scale
                w = raw_image.Width;
                h = raw_image.Height;
            }

            if (w < max_w)
                offset_x = (max_w -w) / 2;
            else
                offset_x = 0;

            if (h < max_h)
                offset_y = (max_h - h) / 2;
            else
                offset_y = 0;

            raw_image.Scale (processed_image, offset_x, offset_y, w, h, offset_x, offset_y,
                (double) w / (double) raw_image.Width, (double)h /(double) raw_image.Height, InterpType.Hyper);

            LoadFromPixelData (processed_image.Pixels,
                processed_image.NChannels == 3 ? PixelFormat.PIXBUF_RGB : PixelFormat.PIXBUF_ARGB,
                processed_image.Width, processed_image.Height,
                processed_image.Rowstride, processed_image.NChannels);

            raw_image.Dispose ();
            processed_image.Dispose ();
        }
开发者ID:GNOME,项目名称:mistelix,代码行数:81,代码来源:SlideImage.cs


注:本文中的Gdk.Pixbuf.Scale方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。