# FXRuby Image Scaling question

**URL:** <https://www.ruby-forum.com/t/fxruby-image-scaling-question/100336>\
**Category:** Ruby\
**Created:** [February 2, 2006, 4:24pm UTC](https://www.ruby-forum.com/t/fxruby-image-scaling-question/100336 "2006-02-02T16:24:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrickharlan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@patrickharlan](https://www.ruby-forum.com/u/patrickharlan)\
**Post date:** [February 2, 2006, 4:24pm UTC](https://www.ruby-forum.com/t/fxruby-image-scaling-question/100336/1 "2006-02-02T16:24:32Z")

</div>

I have a screen with a logo on it, I would like the logo to scale with  
screen size. I have tried more than a couple variations on:

```
# Construct an image
@logo = FXPNGImage.new(getApp(), File.open("logo.png", "rb").read,

```

IMAGE\_KEEP)

```
# Prep an image Frame
image = FXImageFrame.new(self, @logo, LAYOUT_FILL_X | LAYOUT_FILL_Y)
image.backColor = FXColor::Blue

image.connect(SEL_CONFIGURE) do |sender, sel, evt|
  if image.width > 100 && image.height > 100
    @logo.scale(image.width, image.height)
  end
end

```

This works, but the image “degrades” from all the scaling. How would I  
go about keeping the original image? Any sort of “dup” on the FXImage  
results in an exception (at least for me).

Thanks in advance  
pth

---

<div class="post-metadata">

**Author:** ![patrickharlan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@patrickharlan](https://www.ruby-forum.com/u/patrickharlan)\
**Post date:** [February 2, 2006, 7:23pm UTC](https://www.ruby-forum.com/t/fxruby-image-scaling-question/100336/2 "2006-02-02T19:23:10Z")

</div>

On 2/2/06, Patrick H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks in advance  
> pth

Ok I am still very interested if there is a better solution, but this  
works for me:

```
# Construct an image
@logo = FXPNGImage.new(getApp(), File.open("logo.png", "rb").read,

```

IMAGE\_KEEP)  
stream = FXMemoryStream.open(FXStreamSave,nil)  
@logo.savePixels(stream)  
puts stream  
@buffer = stream.takeBuffer  
stream.close  
p @buffer

```
# Prep an image Frame
image = FXImageFrame.new(self, @logo, LAYOUT_FILL_X | LAYOUT_FILL_Y)
image.backColor = FXColor::Blue

image.connect(SEL_CONFIGURE) do |sender, sel, evt|
  if image.width > 100 && image.height > 100
    stream = FXMemoryStream.open(FXStreamLoad, @buffer)
    @logo.loadPixels(stream)
    @logo.scale(image.width, image.height)
    stream.close
  end
end

```

end

---

<div class="post-metadata">

**Author:** ![patrickharlan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@patrickharlan](https://www.ruby-forum.com/u/patrickharlan)\
**Post date:** [February 2, 2006, 8:20pm UTC](https://www.ruby-forum.com/t/fxruby-image-scaling-question/100336/3 "2006-02-02T20:20:36Z")

</div>

On 2/2/06, Patrick H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > image.backColor = FXColor::Blue
> > 
> > ```
> 
> IMAGE\_KEEP)

At the risk of talking to myself too much. Here is the cleaned up  
solution:

class FXScaledImageFrame \< FXImageFrame  
def initialize(parent, img, opts =  
FRAME\_SUNKEN|FRAME\_THICK,x=0,y=0,w=0,h=0,pl=0,pr=0,pt=0,pb=0)  
@img = img  
FXMemoryStream.open(FXStreamSave, nil) do |stream|  
@img.savePixels(stream)  
@buffer = stream.takeBuffer  
end

```
super

self.connect(SEL_CONFIGURE) do |sender, sel, evt|
  if width > 1 and height > 1
    FXMemoryStream.open(FXStreamLoad, @buffer) do |stream|
      @img.loadPixels(stream)
    end
    @img.scale(width, height)
  end
end

yield self if block_given?

```

end  
end
